2011-11-01 19 views
2

我一直收到当使用RSpec的共享示例时,为什么我的本地变量未定义?

friend_or_open_profile_view_spec.rb:14:in 'block in <top (required)>': undefined local variable or method 'another_user' for #<Class:0x007f9d95f16668> (NameError)

我能够通过spec的其他地方使用another_user变量就好了。我错过了什么?

另外,有没有更好的方法来做到这一点?配置文件(取决于用户的状态)将显示不同的组件。我最终希望将所有这些组件移动到可根据上下文调用的共享示例中。

require 'spec_helper' 

describe "viewing a friend's or an open profile" do 

    let(:user)   { Factory(:user) } 
    let(:another_user) { Factory(:user) } 

    before do 
    sign_in user 
    User.stub!(:find).and_return(another_user) 
    end 

    context "when a profile is marked private" do 
    it_behaves_like "a restricted profile", another_user 
    end 
+0

你能证明你的共享的例子吗?如果你写'it_behaves_like“受限制的配置文件”,Factory(:user)'? – nathanvda

回答

0

我刚刚发现了include_examples,使得共享示例在当前上下文中运行。这可能是我所需要的...

+0

'include_examples'只是'it_behaves_like_a'的别名 – nathanvda

0

这将工作:

require 'spec_helper' 

describe "viewing a friend's or an open profile" do 

    before do 
    sign_in user 
    User.stub!(:find).and_return(another_user) 
    end 

    context "when a profile is marked private" do 
    it_behaves_like "a restricted profile" do 
     let(:user)   { Factory(:user) } 
     let(:another_user) { Factory(:user) } 
    end 
    end 
end 
相关问题