Skip to content

Instantly share code, notes, and snippets.

@KevM
Created October 10, 2011 21:15
Show Gist options
  • Select an option

  • Save KevM/1276570 to your computer and use it in GitHub Desktop.

Select an option

Save KevM/1276570 to your computer and use it in GitHub Desktop.
You can mock anything at Zombo com!
[TestFixture]
public abstract class Context<T> where T : class
{
public RhinoAutoMocker<T> _services { get; private set; }
public T _cut { get; private set; }
[SetUp]
public void Setup()
{
_services = new RhinoAutoMocker<T>(MockMode.AAA);
OverrideMocks();
_cut = _services.ClassUnderTest;
Given();
}
public SERVICE MockFor<SERVICE>() where SERVICE : class
{
return _services.Get<SERVICE>();
}
public SERVICE Override<SERVICE>(SERVICE with) where SERVICE : class
{
_services.Inject<SERVICE>(with);
return with;
}
public virtual void Given() { }
public virtual void OverrideMocks() { }
}
[TestFixture]
public class get_with_uncached_specs : Context<CachingSearchDomainSpecficationsService>
{
private IEnumerable<DocumentSpecification> _retrievedSpecs;
private DocumentSpecification[] _specificaitons;
private DateTime _lastRetrieved;
public override void Given()
{
_specificaitons = new[] { new DocumentSpecification { Domain = "domain1" } };
MockFor<ISearchClient>().Stub(a => a.GetSearchDomains()).Return(_specificaitons);
_lastRetrieved = new DateTime(1, 2, 3, 4, 5, 6);
MockFor<IAppServerTime>().Stub(a => a.Now).Return(_lastRetrieved);
_retrievedSpecs = _cut.Get();
}
[Test]
public void specs_from_search_client_are_returned()
{
_retrievedSpecs.ShouldBeTheSameAs(_specificaitons);
}
[Test]
public void should_set_last_time_retrieved()
{
_cut.LastRetrivalOfDocumentSpecifications.ShouldEqual(_lastRetrieved);
}
[Test]
public void should_retrieve_document_specs()
{
MockFor<ISearchClient>().AssertWasCalled(a => a.GetSearchDomains());
}
}
MockFor<IService>().Stub(a => a.Method(Arg<string>.Is.Anything));
//Below is a helper I like to use which drives an AutoMockingContainer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment