Skip to content

Instantly share code, notes, and snippets.

@gasparnagy
Last active February 2, 2017 08:55
Show Gist options
  • Save gasparnagy/b80d157e01b0120437cbe6a426e67558 to your computer and use it in GitHub Desktop.
Save gasparnagy/b80d157e01b0120437cbe6a426e67558 to your computer and use it in GitHub Desktop.
Code Examples for Post "SpecFlow Tips: Baseclass or Context Injection"
public class SharedStepsBaseClass : Steps
{
protected UserContext UserContext
{
get
{
var result = (UserContext)ScenarioContext["userContext"];
if (result == null)
{
result = new UserContext();
ScenarioContext["userContext"] = result;
}
return result;
}
}
}
[Binding]
public class ShareDataWithBaseClassSteps2 : SharedStepsBaseClass
{
[When(@"the user opens the assigned tasks")]
public void WhenTheUserOpensTheAssignedTasks()
{
// Read the user information from the scenario context through the
// property declared in the base class.
var tasks = TaskManagement.GetAssignedTasks(UserContext.UserName);
//...
}
}
[Binding]
public class ShareDataWithContextInjectionSteps1 : Steps
{
private readonly UserContext _userContext;
public ShareDataWithContextInjectionSteps1(UserContext userContext)
{
_userContext = userContext;
}
[Given(@"an Administrator has logged in")]
public void GivenAnAdministratorHasLoggedIn()
{
var userName = UserManagement.CreateAdminUser();
UserManagement.Login(userName);
// Save data about the current user into the injected user context
_userContext.UserName = userName;
_userContext.Role = "Administrator";
}
}
[Binding]
public class ShareDataWithContextInjectionSteps2 : Steps
{
private readonly UserContext _userContext;
public ShareDataWithContextInjectionSteps2(UserContext userContext)
{
_userContext = userContext;
}
[When(@"the user opens the assigned tasks")]
public void WhenTheUserOpensTheAssignedTasks()
{
// Read the user information from the injected user context
var tasks = TaskManagement.GetAssignedTasks(_userContext.UserName);
//...
}
}
[Binding]
public class ShareDataWithInstanceFieldsSteps
{
private UserContext _userContext;
[Given(@"an Administrator has logged in")]
public void GivenAnAdministratorHasLoggedIn()
{
var userName = UserManagement.CreateAdminUser();
UserManagement.Login(userName);
// Save data about the current user into an instance field
_userContext = new UserContext
{
UserName = userName,
Role = "Administrator"
};
}
[When(@"the user opens the assigned tasks")]
public void WhenTheUserOpensTheAssignedTasks()
{
// Read the user information from the instance field
var currentUserName = _userContext.UserName;
var tasks = TaskManagement.GetAssignedTasks(currentUserName);
//...
}
}
[Binding]
public class ShareDataWithScenarioContextSteps1 : Steps
{
[Given(@"an Administrator has logged in")]
public void GivenAnAdministratorHasLoggedIn()
{
var userName = UserManagement.CreateAdminUser();
UserManagement.Login(userName);
// Save data about the current user into the current scenario context
// We use the ScenarioContext property defined in the Steps base class
// instead of ScenarioContext.Current, because ScenarioContext.Current
// does not work for parallel execution.
ScenarioContext["userContext"] = new UserContext
{
UserName = userName,
Role = "Administrator"
};
}
}
[Binding]
public class ShareDataWithScenarioContextSteps2 : Steps
{
[When(@"the user opens the assigned tasks")]
public void WhenTheUserOpensTheAssignedTasks()
{
// Read the user information from the current scenario context
var userContext = (UserContext)ScenarioContext["userContext"];
var tasks = TaskManagement.GetAssignedTasks(userContext.UserName);
//...
}
}
public class SharedStepsBaseClassResolvingInjectedObjects : Steps
{
protected UserContext UserContext
{
get
{
return ScenarioContext.ScenarioContainer.Resolve<UserContext>();
}
}
}
public class UserContext
{
public string UserName { get; set; }
public string Role { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment