2017-03-01 155 views
0

我對RSpec很新。我正在嘗試爲Rails應用程序做一個基本的控制器測試。我想檢查一下,當我使用PostsController的「創建」操作時,用戶被重定向到posts路徑。這是我目前的測試。RSpec控制器測試錯誤

describe "POST #create" do 
    it "returns http success" do 
    post :create, {'post' => { :title => "My first post", :author => 'Jack Seabolt'} } 
    expect(response). to have_http_status(:success) 
    end 

    it "redirects to 'index' template" do 
    post :create , {'post' => {:title => "my first post", :author => 'Jack Seabolt'} } 
    expect(response) redirect_to(posts_path) 
    end 
end 

此代碼導致此錯誤:

/Users/johnseabolt/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load':  
/Users/johnseabolt/Desktop/projects/tdd/spec/controllers/posts_controller_spec.rb:49: syntax error, unexpected tIDENTIFIER, expecting keyword_end (SyntaxError) 
    expect(response) redirect_to(posts_path) 
          ^

我試圖將只是 '帖子'。我試過'索引'。沒有工作。如果有人能指引我正確的方向,我將不勝感激。謝謝。

+0

與尋找解決任何運氣爲t他? –

回答

0

你已經錯過來電toexpect的方法:

expect(response).to redirect_to(posts_path) 
0

通常對於一個期望的公式是這樣的:

expect(something).to eq another_thing 
expect(something).to be another_thing 

expect(something).to_not eq another_thing 
expect(something).to_not be another_thing 

在你情況下,您不需要beeq匹配器的原因redirect_to

expect(response).to redirect_to(posts_path) 

DOCS:

文檔爲redirect_to匹配:https://www.relishapp.com/rspec/rspec-rails/v/3-5/docs/matchers/redirect-to-matcher

文檔爲be匹配器:https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/be-matchers

文檔爲eq匹配器:https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/equality-matchers