简体   繁体   English

RSpec控制器测试错误

[英]RSpec Controller Testing Error

I am very new to RSpec. 我对RSpec非常陌生。 I am trying to do a basic controller test for Rails app. 我正在尝试对Rails应用进行基本的控制器测试。 I want to check that when I use the PostsController's "create" action, the user gets redirected to the posts path. 我想检查一下,当我使用PostsController的“ create”操作时,用户是否被重定向到posts路径。 Here is my current test. 这是我目前的测试。

 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 

This code leads to this error: 此代码导致此错误:

/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)
                              ^

I've attempting adding just 'posts'. 我试图仅添加“帖子”。 I tried 'index'. 我尝试了“索引”。 Didn't work. 没用 If someone could point me in the right direction, I would appreciate it. 如果有人能指出我正确的方向,我将不胜感激。 Thanks. 谢谢。

你已经错过了调用一个方法to用于expect

expect(response).to redirect_to(posts_path)

Usually the formula for an expectation is something like: 通常,期望的公式如下:

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

or 要么

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

In your case, you don't need the be or eq cause of the redirect_to matcher. 在您的情况下,您不需要redirect_to匹配器的beeq原因。

expect(response).to redirect_to(posts_path)

DOCS: DOCS:

docs for redirect_to matcher: https://www.relishapp.com/rspec/rspec-rails/v/3-5/docs/matchers/redirect-to-matcher redirect_to匹配器的文档: https : //www.relishapp.com/rspec/rspec-rails/v/3-5/docs/matchers/redirect-to-matcher

docs for be matchers: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/be-matchers 对于文档be的匹配: https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/be-matchers

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM