使用Nancy.Testing.Browser发布时出现ConfigurableBootstrapper错误

时间:2013-03-11 15:18:03

标签: unit-testing nancy

我有一个简单的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);
}

这是我的测试Bootstrapper:

public class NancyBootstrapper : ConfigurableBootstrapper
{
    public NancyBootstrapper()
        : base(with => { with.Module<CarsModule>(); })
    { }

    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        container.Register<ICarRepository>(new FakeData.CarRepository());
    }
}

我是否必须在ConfigurableBootstrapper中执行任何特殊操作才能使绑定生效?

2 个答案:

答案 0 :(得分:6)

仔细检查您的packages.config是否正在推出正确的版本。

更新:因为这主要来自@StevenRobbins,所以我在背后给了他一个轻拍:

enter image description here

答案 1 :(得分:2)

@Jon检查packages.config的建议是正确的。尽管使用相同的方法在几分钟之内添加了包,但测试项目的Nancy版本比Web项目更旧。更新到正确版本,现在工作正常。