2017-12-17 7 views
0

私のブートキャンプ用のプロジェクトで作業しています。なぜテストが失敗するのかわかりませんが、その動作は正しく動作します。テスト:Ruby on Railsアクションは動作しますがテストは失敗します

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 

createアクション:

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' 

は私が3つの失敗を取得します。

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 

にエラーが発生し、未知のキーワード「トピック」の問題ですまだそのキーワードが私のtopics_controller_spec.rbファイル内の他の7回のテストで正常に動作することを私に言っています。私は1時間以上これを見つめており、私が間違っていた私の人生については見分けることができません。

答えて

1

あなたは古い例を選んだと思います。

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 
関連する問題