2009-11-26 69 views
10

我想用RSpec來測試我的視圖。RSpec查看測試:如何修改params?

link_to "sort>name", model_path(:sort_by => 'name')導致http://mydomain/model?sort_by=name

我的看法,然後使用這個參數那樣:

<% if params[:sort_by] == 'name' %> 
<div>Sorted by Name</div> 
<% end %> 

RSpec的樣子,是造成我的煩惱特定視圖取決於URL參數改變其外觀這個:

it "should tell the user the attribute for sorting order" do 
    #Problem: assign params[:sort_for] = 'name' 
    render "/groups/index.html.erb" 
    response.should have_tag("div", "Sorted by Name") 
end 

我想在RSpec中測試我的視圖(沒有控制器),但我不能得到這個參數呃進入我的params變量。我試圖在所有不同的口味assign

  • assign[:params] = {:sort_by => 'name'}
  • assign[:params][:sort_by] = 'name'
  • ...

沒有成功爲止。每一個想法是讚賞。

回答

10

這是因爲你不應該在你的意見中使用params
我看到它使用助手的最佳方式。

<div>Sorted by <%= sorted_by %></div> 

而在你的幫助文件中的一個

def sorted_by 
    params[:sorted_by].capitalize 
end 

然後你就可以很方便地測試你的助手(因爲在助手的測試,你可以定義params請求。

+0

不是我所期望的,但它解決了我的問題並改進了我的應用程序。謝謝! – sebastiangeiger 2009-11-26 10:53:50

33

如果它的控制器測試那麼它將是

controller.stub!(:params).and_return {} 

如果它的幫助測試那麼它會b E:

helper.stub!(:params).and_return {} 

而且其視圖測試將是:

view.stub!(:params).and_return {} 

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

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(:params).and_return({sort_by: 'name'}) 
+0

我發現這個很有幫助,但是我使用了Rspec 3語法:'allow(view).to receive(:params).and_return({action:'index'})' – 2015-09-25 19:14:21

5

最簡單的辦法是隻做到這一點:

helper.params = {:foo => '1', :bar => '2'} 

但一般來說,最好是更多的整合-Y,而不是「存根「值的時候是可行的。所以我更願意使用integrate_views的控制器測試。然後你可以指定你的參數爲得到,並測試整個流程,從發送params到控制器,讓它們由控制器處理,最後到渲染。

我一般還喜歡將視圖邏輯拉入助手,這可以更容易測試。

例如,說我有一個稱爲selection_list幫手,它返回一個散列其「selected_preset」鍵依賴於PARAMS:如果對於參數是指定一個空值,默認值爲42 [selected_preset]。

這裏是一個控制器測試,我們稱之爲integrate_views(當然,你也可以用一個實際的視圖測試做同樣的事情,如果你喜歡的話)。

describe '#show' do 
    describe 'selected_preset' do 
    it 'should default to 42 if no value was entered' do 
     get :show, :params => {:selected_preset => ''} 
     response.template.selection_list[:selected_preset].should == 42 

如果此功能的某些部分發生中斷,此集成測試將提醒我。但我也希望有一些單位測試,以幫助我確定破損。

我將首先讓助手使用實例變量而不是直接訪問參數。我將通過添加正下方的得到一個單一的線改變上面的代碼,如下所示:

describe '#show' do 
    describe 'selected_preset' do 
    it 'should default to 42 if no value was entered' do 
     get :show, :params => {:selected_preset => ''} 
     assigns[:selected_preset].should == 42 # check instance variable is set 
     response.template.selection_list[:selected_preset].should == 42 

現在我也可以容易地執行一個輔助單元測試:

describe MyHelper do 
    describe '#selection_list' do 
    it 'should include the selected preset' do 
     assigns[:selected_preset] = 3 
     helper.selection_list[:selected_preset].should == 3 
+0

現在看來「integrate_views」現在已被棄用「render_views」。 https://relishapp.com/rspec/rspec-rails/v/2-1/docs/controller-specs/render-views – 2018-02-07 22:24:50

1

設定圖的另一種方法params:

controller.request.path_parameters[:some_param] = 'a value'