我有一个简单的 NancyModule 声明了一个 Post :
Post["/Car/New"] = args =>
{
Car newCar = this.Bind<Car>();
newCar = _carRepos.CreateNewCar(newCar);
return Response.AsJson<Car>(newCar);
};
从视图发布到此效果很好:
<form action="/Car/New" method="post">
<input type="text" name="colour" />
<input type="submit" value="Submit" />
</form>
当我尝试对此路线进行测试时,我收到以下错误:
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)
这是我的测试:
[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);
}
这是我的测试引导程序:
public class NancyBootstrapper : ConfigurableBootstrapper
{
public NancyBootstrapper()
: base(with => { with.Module<CarsModule>(); })
{ }
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
container.Register<ICarRepository>(new FakeData.CarRepository());
}
}
我是否必须在我的 ConfigurableBootstrapper 中做任何特别的事情才能使绑定工作?