2013-01-06 18 views
0

我已經在我的application_helper_spec.rbrspec的設()行爲

shared_examples_for "some shared process" do 

    it "displays the value defined by Let" do 
     puts object.inspect # This should output the value defined by Let 
    end 

end 

describe ApplicationHelper do 

    describe "a_helper_methond" do 

     describe "some behaviour" do 
      let(:object) { "foo" } 
      puts object.inspect # This should output the value defined by Let 
      it_should_behave_like "some shared process" 
     end 

    end 

end 

然而,當我運行此我得到兩個放兩個錯誤如下:

undefined local variable or method `object' for #<Class:0x007f951a3a7b20> (NameError) 

爲什麼?我的模型中有完全相同的代碼,並且請求規範在運行良好,但在幫助程序規範中沒有。

回答

4

let調用在您的規格將被執行的上下文中定義了一種方法。然而,您的puts聲明超出了該範圍。您需要將其包裝在it塊中。

it 'print debug output' do 
    puts object.inspect 
end 
0

我還進一步通過把設命令it_behaves_like塊內改進本