forked from xerxesb/SpecFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Context Injection
gasparnagy edited this page Aug 15, 2012
·
6 revisions
SpecFlow supports a very simple dependency framework that is able to instantiate and inject class instances for the scenarios. With this feature you can group the shared state to context-classes, and inject them into every binding class that is interested in that shared state.
The life-time of these classes are limited to a scenario execution. The injection is resolved recursively, i.e. the injected class can also have dependencies. The resolution is done using the public constructors only. If there are multiple public constructors, SpecFlow takes the first one.
##Examples
The following example defines a context class to store referred books. The context class is injected to a binding class.
public class CatalogContext
{
public CatalogContext()
{
ReferenceBooks = new ReferenceBookList();
}
public ReferenceBookList ReferenceBooks { get; set; }
}
[Binding]
public class BookSteps
{
private readonly CatalogContext _catalogContext;
public BookSteps(CatalogContext catalogContext)
{
_catalogContext = catalogContext;
}
[Given(@"the following books")]
public void GivenTheFollowingBooks(Table table)
{
foreach (var book in table.CreateSet<Book>())
{
SaveBook(book);
_catalogContext.ReferenceBooks.Add(book.Id, book);
}
}
}