2014-09-26 82 views
0

我試圖添加一個新的類到我的標題模板的一部分,如果應用程序正在移動設備上查看,我得到這個錯誤從耙子(但它完美地工作在本地主機):rspec未定義的方法`mobile_device?'對於#<#類

失敗/錯誤:render:template =>「/layouts/_header.html.erb」 ActionView :: Template :: Error: 未定義的方法`mobile_device?'爲#<#:0x007fbbddbd0b50>

這是我的頭模板引起問題的行:

./app/views/layouts/_header.html.erb

<div class="<%= 'flexslider-mobile' if mobile_device? && !current_page?('/') %>"> 
    <some html> 
</div> 

而且這是我在我的測試中呈現模板時卡住的線:

./spec/views/layouts/application_spec

require 'spec_helper' 

describe 'header' do 

    before(:each) do 
    render :template => "/layouts/_header.html.erb" 
    end 

./app/controllers/application_controller.rb

class ApplicationController < ActionController::Base 

    protected 

def mobile_device? 
    if session[:mobile_param] 
     session[:mobile_param] == "1" 
    else 
     request.user_agent =~ /Mobile|webOS/ 
    end 
    end 
    helper_method :mobile_device? 
end 

我新的軌道,並RSPEC,所以我敢肯定這件事情很明顯,我只是不知道.. 。謝謝你的幫助!

+0

你試過方法mobile_device嗎?放棄保護? – 2014-09-26 21:43:00

+0

試圖將其移出保護區域,這沒有什麼區別。不清楚你的意思是「method mobile_device?」你可以解釋嗎?謝謝 – COS 2014-09-27 11:34:14

+0

我只是想移動mobile_device?出於保護..這就是它 – 2014-09-27 17:39:22

回答

0

原來視圖測試沒有控制範圍內,所以我需要渲染之前存根方法:

before(:each) do 
    view.stub("mobile_device?" => false) 
    render :template => "/layouts/_header.html.erb" 
    end 

的偉大工程。感謝danpickett在stackoverflow之外給予他的幫助!

0

helper_method調用必須在protected關鍵字之上聲明。

+0

我已經試過了,但仍然得到失敗的測試,可悲的。 – COS 2014-10-01 14:12:50

相關問題