Skip to content

Instantly share code, notes, and snippets.

@ryansroberts
Created August 23, 2010 11:23
Show Gist options
  • Save ryansroberts/545277 to your computer and use it in GitHub Desktop.
Save ryansroberts/545277 to your computer and use it in GitHub Desktop.
public class AutoMockingContext<TUnderTest> where TUnderTest : class
{
protected static MoqAutoMocker<TUnderTest> autoMocker;
Establish automocking = () => autoMocker = new MoqAutoMocker<TUnderTest>();
protected static Mock<TInterface> Stub<TInterface>() where TInterface : class
{
var mocked = autoMocker.Get<TInterface>();
return Mock.Get(mocked);
}
}
public class HandlerContext<TResource, THandler>:AutoMockingContext< THandler>
where THandler : ResourceController
{
protected static ResourceResult result;
static HandlerContext()
{
RouteTable.Routes.Clear();
RouteTable.Routes.FromAssemblyWithType<THandler>();
}
protected static TResource Resource
{
get { return (TResource) result.Resource; }
}
protected static void Has_expected_resource_type()
{
Resource.GetType().ShouldEqual(typeof(TResource));
}
protected static void Get(string uri)
{
..gubbins
}
}
[Subject(typeof(TermsController))]
public class TermsControllerSpec
{
public class When_searching_for_a_term_by_id_that_does_not_exist : HandlerContext<TermResource,TermsController>
{
Establish context = () => Stub<ITermService>()
.Setup(s => s.ById(MoqIt.Is<IEnumerable<int>>(p => p.First() == 1)))
.Returns(new TermResource[]{});
Because of = () => Get("tagging/terms/1");
Behaves_like<A_request_for_a_resource_that_does_not_exist> get;
}
public class When_searching_for_a_term_by_id_that_exists : HandlerContext<TermResource, TermsController>
{
Establish context = () => Stub<ITermService>()
.Setup(s => s.ById(MoqIt.Is<IEnumerable<int>>(p => p.First() == 1)))
.Returns(new[]{new TermResource(1, "exists")});
Because of = () => Get("tagging/terms/1");
Behaves_like<A_request_for_a_resource_that_exists> get;
It Matches_the_requested_id = () => Resource.Id.ShouldEqual(1);
It Matches_the_semaphore_term_name = () => Resource.Name.ShouldEqual("exists");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment