0
我想学习TDD,这是我作业的一部分,我无法弄清楚如何去做。Rspec创建控制器操作的测试有什么问题?
我想测试create
控制器动作,这里是我的测试代码:
require 'spec_helper'
describe MoviesController do
describe 'create' do
it 'should call the model method perform create!' do
Movie.should_receive(:create!).with({"title" => 'Milk', "rating" => 'R'})
post :create, :movie => {:title => 'Milk', :rating => 'R'}
end
end
end
但我得到:
Failures:
1) MoviesController create should call the model method performe create!
Failure/Error: post :create, :movie => {:title => 'Milk', :rating => 'R'}
NoMethodError:
undefined method `title' for nil:NilClass
# ./app/controllers/movies_controller.rb:50:in `create'
# ./spec/controllers/movies_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.21714 seconds
这里是创建行动,我对测试。是的,这是TDD,是的,我测试 工作代码,并且它的测试不工作:d
def create
@movie = Movie.create!(params[:movie])
flash[:notice] = "#{@movie.title} was successfully created."
redirect_to movies_path
end
我甚至不知道为什么我得到了未定义的方法错误讯息?我还有其他几个测试通过了,但为了简单起见,我在这段代码片段中删除了,所以我不认为它与db/model相关的配置问题有关。但为什么它不工作,以及如何改变它?
干杯
Should_receive将返回零,即使它没有收到的匹配参数“{”标题“=>‘牛奶’,‘等级’=>‘R’}”,所以我需要赶上返回值,我知道了吗? – 2012-03-31 03:22:20
对不起,我现在明白了,它来自控制器线。谢谢@ctide,拯救了我的一天! – 2012-03-31 03:28:53