我正在为我的训练营开展一个项目,我无法理解为什么测试失败但行动正常。测试:
describe "POST create" do
it "increases the number of topics by 1" do
expect{ post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}}.to change(Topic,:count).by(1)
end
it "assigns Topic.last to @topic" do
post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}
expect(assigns(:topic)).to eq Topic.last
end
it "redirects to the new topic" do
post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}
expect(response).to redirect_to Topic.last
end
end
创建动作:
def create
@topic = Topic.new
@topic.name = params[:topic][:name]
@topic.description = params[:topic][:description]
@topic.public = params[:topic][:public]
if @topic.save
redirect_to @topic, notice: "Topic was saved successfully."
else
flash.now[:alert] = "Error creating topic. Please try again."
render :new
end
end
我查看我的测试:
rspec spec/controllers/topics_controller_spec.rb -e 'POST create'
我有三次失败。
Failures:
1) TopicsController POST create increases the number of topics by 1
Failure/Error: expect{ post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph }}}.to change(Topic, :count).by(1)
ArgumentError:
unknown keyword: topic
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/topics_controller_spec.rb:54:in `block (4 levels) in <top (required)>'
# ./spec/controllers/topics_controller_spec.rb:54:in `block (3 levels) in <top (required)>'
2) TopicsController POST create assigns Topic.last to @topic
Failure/Error: post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}
ArgumentError:
unknown keyword: topic
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/topics_controller_spec.rb:58:in `block (3 levels) in <top (required)>'
3) TopicsController POST create redirects to new topic
Failure/Error: post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}
ArgumentError:
unknown keyword: topic
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/topics_controller_spec.rb:63:in `block (3 levels) in <top (required)>'
Finished in 0.02654 seconds (files took 2.16 seconds to load)
3 examples, 3 failures
Failed examples:
rspec ./spec/controllers/topics_controller_spec.rb:53 # TopicsController POST create increases the number of topics by 1
rspec ./spec/controllers/topics_controller_spec.rb:57 # TopicsController POST create assigns Topic.last to @topic
rspec ./spec/controllers/topics_controller_spec.rb:62 # TopicsController POST create redirects to new topic
该错误告诉我,未知关键字“topic”存在问题,但该关键字在我的topics_controller_spec.rb文件中的其他7个测试中运行正常。我一直盯着这个看了一个多小时,无法理解我出错的生活。
我想你选了一个旧例子。
您需要使用params键添加params
。试试这个
describe "POST create" do
it "increases the number of topics by 1" do
expect{ post :create, params: {topic: ...} }.to change(Topic,:count).by(1)
end
end