Ruby on rails 为什么在使用RSpec';分享的例子是什么?

Ruby on rails 为什么在使用RSpec';分享的例子是什么?,ruby-on-rails,testing,rspec,rspec2,Ruby On Rails,Testing,Rspec,Rspec2,我一直在 friend_或_open_profile_view_spec.rb:14:in'block in':未定义的局部变量或方法#(namererror) 我可以通过规范在其他地方使用另一个用户变量。我错过了什么 还有,有没有更好的方法?根据用户的状态,配置文件将显示不同的组件。我最终希望将所有这些组件移动到共享示例中,这些示例可以根据上下文调用 require 'spec_helper' describe "viewing a friend's or an open profile"

我一直在

friend_或_open_profile_view_spec.rb:14:in'block in':未定义的局部变量或方法#(namererror)

我可以通过规范在其他地方使用另一个用户变量。我错过了什么

还有,有没有更好的方法?根据用户的状态,配置文件将显示不同的组件。我最终希望将所有这些组件移动到共享示例中,这些示例可以根据上下文调用

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

我刚刚发现include_示例可以在当前上下文中运行共享示例。这可能是我所需要的…

我刚刚发现了include\u示例,以便共享示例在当前上下文中运行。这可能是我所需要的…

这将起作用:

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
这将有助于:

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

include\u examples
只是
的别名,它的行为类似于\u a
include\u examples
只是
它的行为类似于\u a
的别名,您能展示一下您的共享示例吗?如果您编写
它的行为类似于“受限配置文件”,Factory(:user)
,它是否有效?您能展示一下您的共享示例吗?如果你写
它的行为像“受限配置文件”,工厂(:user)
,它能工作吗?