2013-05-22 207 views
0

我正在开发一个Rails 3.2应用程序,测试覆盖率,但由于某些原因,我的一些测试失败。相同的规格适用于另一种型号。Stange Rspec测试失败

规格与Rspec,FactoryGirl,Shoulda-Matchers一起运行。

这是失败规格:http://pastebin.com/8VWCAv79

的错误是:

Failures: 

    1) PlacesController POST create when user is logged in with valid params redirects to the created place 
    Failure/Error: response.should redirect_to(Place.last) 
     Expected response to be a <:redirect>, but was <200> 
    # ./spec/controllers/places_controller_spec.rb:109:in `block (5 levels) in <top (required)>' 

    2) PlacesController POST create when user is logged in with valid params assigns a newly created place as @place 
    Failure/Error: assigns(:place).should be_persisted 
     expected persisted? to return true, got false 
    # ./spec/controllers/places_controller_spec.rb:104:in `block (5 levels) in <top (required)>' 

    3) PlacesController POST create when user is logged in with valid params creates a new Place 
    Failure/Error: expect { 
     count should have been changed by 1, but was changed by 0 
    # ./spec/controllers/places_controller_spec.rb:96:in `block (5 levels) in <top (required)>' 

Finished in 4.63 seconds 
21 examples, 3 failures 

Failed examples: 

rspec ./spec/controllers/places_controller_spec.rb:107 # PlacesController POST create when user is logged in with valid params redirects to the created place 
rspec ./spec/controllers/places_controller_spec.rb:101 # PlacesController POST create when user is logged in with valid params assigns a newly created place as @place 
rspec ./spec/controllers/places_controller_spec.rb:95 # PlacesController POST create when user is logged in with valid params creates a new Place 

的完全相同的规格出现在另一个文件中,http://pastebin.com/r8HtAwSR,并且正确地传递,没有问题。

这些控制器文件:

任何人都可以建议我如何解决这个问题呢?

回答

0

好吧,它看起来像新的地方不是由于某种原因而创建的(我的猜测是,它没有通过验证)。

尝试调试您的测试。我喜欢使用pry。只需添加到您的gem文件gem 'pry',运行捆绑软件,然后您可以在执行中设置断点。

例如试试这个:

it "redirects to the created place" do 
    post :create, {:place => valid_attributes} 
    binding.pry 
    response.should redirect_to(Place.last) 
end 

执行将停止在该点,在那里你插入“binding.pry”,你可以查阅一下在你的终端最后的地方。

+0

不幸的是,这并没有帮助解决问题......奇怪的是,模型通过了所有的验证测试,这些测试使用控制器中使用的同一工厂运行,所以属性等于... – endorama

+0

@ endorama只是把'binding.pry'不会解决问题 - 当然。重点是一旦你有了binding.pry,那么你可以在那里探索你的程序来找出发生了什么,你可以'cd'进出对象,显示方法的来源,运行代码等等。你可以访问到那时您的整个计划的状态,并且您可以更好地找到问题的根源。 – horseyguy

+0

@endorama它不能解决你的问题。这是想帮助你找出问题所在。在测试中放置'绑定pry',运行你的测试套件,执行将停止在你插入绑定的位置,改变到你的终端(到你的服务器运行的地方),然后尝试'Place.last'。这是你所期望的吗?如果没有检查你的valid_attributes。他们真的有效吗?如果你做了'place = Place.new(valid_attributes); place.valid ?;”。如果为false,请检查您的验证错误'place.errors'是否正确,问题在其他地方...... –