2015-08-16 23 views
1

在我看來,我有以下代碼如何在rails rspec中存儲action_name。

<h2><%= action_name.capitalize %> User</h2> 

在我的測試中,我有以下

RSpec.describe 'users/new.html.erb', type: :view do 

it 'displays the form to create a new user' do 
assign :user, build(:user) 

render 

expect(rendered).to match 'Upload Image' 
end 
end 

當我跑我得到以下

undefined method `capitalize' for nil:NilClass 

如何可以將代碼我stub的action_name變量?

回答

1

我現在不能檢查自己,但根據this你可以試試這個代碼

view.stub(:action_name).and_return("my_action_name") 

如果你被警告象下面這樣。

Deprecation Warnings: 

Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /home/akbarbin/Documents/Office/projects/portfolio/spec/views/admin/waste_places/new.html.erb_spec.rb:7:in `block (2 levels) in <top (required)>'. 


If you need more of the backtrace for any of these deprecations to 
identify where to make the necessary changes, you can configure 
`config.raise_errors_for_deprecations!`, and it will turn the 
deprecation warnings into errors, giving you the full backtrace. 

1 deprecation warning total 

Finished in 4.86 seconds (files took 4.72 seconds to load) 

你可以把它變成

allow(view).to receive(:action_name).and_return("my_action_name")