I have a simple NancyModule that has a Post declared:
Post["/Car/New"] = args =>
{
Car newCar = this.Bind<Car>();
newCar = _carRepos.CreateNewCar(newCar);
return Response.AsJson<Car>(newCar);
};
Posting to this from a view works fine:
<form action="/Car/New" method="post">
<input type="text" name="colour" />
<input type="submit" value="Submit" />
</form>
When I try and run a test for this route, I get the following error:
System.Exception : ConfigurableBootstrapper Exception
----> Nancy.RequestExecutionException : Oh noes!
----> System.MissingMethodException : Method not found: '!!0 Nancy.ModelBinding.ModuleExtensions.Bind(Nancy.INancyModule, System.String[])'.
Result StackTrace:
at Nancy.Testing.PassThroughStatusCodeHandler.Handle(HttpStatusCode statusCode, NancyContext context)
at Nancy.NancyEngine.CheckStatusCodeHandler(NancyContext context)
at Nancy.NancyEngine.HandleRequest(Request request, Func`2 preRequest)
at Nancy.NancyEngine.HandleRequest(Request request)
at Nancy.Testing.Browser.HandleRequest(String method, String path, Action`1 browserContext)
at Nancy.Testing.Browser.Post(String path, Action`1 browserContext)
at Shopr.Tests.Cars.CarTests.PostNewCarReturnsCar() in c:\Users\*******\Documents\Visual Studio 2012\Projects\Shopr\Shopr.Tests\Cars\CarTests.cs:line 35
--RequestExecutionException
at Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)
--MissingMethodException
at Shopr.Api.Modules.CarsModule.<.ctor>b__3(Object args)
at Nancy.Routing.Route.Invoke(DynamicDictionary parameters)
at Nancy.Routing.DefaultRouteInvoker.Invoke(Route route, DynamicDictionary parameters, NancyContext context)
at Nancy.Routing.DefaultRequestDispatcher.Dispatch(NancyContext context)
at Nancy.NancyEngine.InvokeRequestLifeCycle(NancyContext context, IPipelines pipelines)
And this is my test:
[Test]
public void PostNewCarReturnsCar()
{
var browser = BrowserFactory.Create();
var response = browser.Post("/Car/New", with =>
{
with.FormValue("Colour", "Red");
});
var car = GetObjectFromJsonBody(response.Body.AsString());
Assert.IsNotNull(car);
Assert.AreEqual(2, car.Id);
}
This is my testing Bootstrapper:
public class NancyBootstrapper : ConfigurableBootstrapper
{
public NancyBootstrapper()
: base(with => { with.Module<CarsModule>(); })
{ }
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
container.Register<ICarRepository>(new FakeData.CarRepository());
}
}
Do I have to do anything special in my ConfigurableBootstrapper to get the binding to work?
Double check that your packages.config are pulling the right versions.
UPDATE: As this mostly came from @StevenRobbins I award him a pat on the back:

The suggestion from @Jon to check packages.config was correct. Even though the packages were added within minutes of each other using the same method, the test project had an older version of Nancy than the web project. Updated to the proper version and it works fine now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With