-
Notifications
You must be signed in to change notification settings - Fork 43
Usage
darrencauthon edited this page Aug 16, 2011
·
2 revisions
Create<T> will instantiate an object, filling any dependencies with mock objects. For example,
public class ClassWithDependencies{
public ClassWithDependencies(IFoo foo, IBar bar){
}
}
var mocker = new AutoMoqer();
var classWithDependencies = mocker.Create<ClassWithDependencies>();
The mocks that are passed to the object are accessible through GetMock<T>.
GetMock<T> allows you to get the mock object that was passed to an object that was instantiated, like this:
public class ClassWithDependencies{
public ClassWithDependencies(IFoo foo, IBar bar){
}
}
var mocker = new AutoMoqer();
var classWithDependencies = mocker.Create<ClassWithDependencies>();
// both of these lines return the same mock that was used to create classWithDependencies
var foo = mocker.GetMock<IFoo>();
var bar = mocker.GetMock<IBar>();
GetMock<T> also allows you to access the mock object before they are used to create an object, like this:
public class ClassWithDependencies{
public ClassWithDependencies(IFoo foo, IBar bar){
}
}
var mocker = new AutoMoqer();
// both of these will be used to create classWithDependencies
var foo = mocker.GetMock<IFoo>();
var bar = mocker.GetMock<IBar>();
var classWithDependencies = mocker.Create<ClassWithDependencies>();
public class ClassWithDependencies{
public ClassWithDependencies(IFoo foo, IBar bar){
}
}
var mocker = new AutoMoqer();
// this will be used as IFoo when creating classWithDependencies
mocker.SetInstance<IFoo>(new Foo());
var classWithDependencies = mocker.Create<ClassWithDependencies>();