2016-04-27 22 views
1

私はApplicationControllerにRailsのRSpecの未定義のメソッド<RSpecの:: ExampleGroups

def set_seo(page) 
    set_meta_tags :site => '', :title => page.seo_title ? page.seo_title : '', :reverse => true, 
       :description => page.seo_description ? page.seo_description : "" 
end 

にset_seo方法を持っており、この方法を含んでいる私のBrandsControllerをテストしたい

def index 
    #some code here 
    set_seo(@content) 
end 

私のコントローラの仕様

require 'spec_helper' 

describe BrandsController do 
    render_views 
    before(:all) do 
    @content = create(:content) 
    create(:user) 
    end 

    describe "GET index" do 
    it "index" do 
     allow(ApplicationController).to receive(set_seo(@content)).and_return(set_seco(@content)) 
     brand = Brand.create 
     get :index 
     expect(response).to render_template(:index) 
    end 

しかし、私は持っている

NoMethodError: 
    undefined method `set_seo' for #<RSpec::ExampleGroups::BrandsController::GETIndex:0x007f866daa96c8> 

答えて

0

set_seoは、仕様には定義されていません。したがって、undefined methodエラーです。メソッドをモック/スタブのシンボルとして渡す必要があります(言い換えれば、メソッドの振る舞いを置き換えたり、直接呼び出さずに "嘲笑"しています)。

これは、この行のポイントは何ですか:allow(ApplicationController).to receive(set_seo(@content)).and_return(set_seco(@content))?すでに返ってきたものを返すように:set_seoに伝えていませんか?

あなたはset_seoが実際に呼び出されることを保証しようとしている場合は、次を使用します。

expect_any_instance_of(ApplicationController).to receive(:set_seo).with(@content).and_call_original 
関連する問題