railstutorial,这是RSpec 3中不支持的不推荐行为

railstutorial,这是RSpec 3中不支持的不推荐行为,rspec,rspec-rails,railstutorial.org,Rspec,Rspec Rails,Railstutorial.org,我在做《铁路史》,现在是第11章 为什么会出现这种错误 警告:让声明在(:all) 钩住: /用户/xxx/Documents/rails\u projects/sample\u app\u 2/spec/requests/microspost\u pages\u spec.rb:49:in `'中的块(4层)' 这是RSpec 3中不支持的不推荐行为 let和subject声明不打算在 before(:all)hook,因为它们用于定义重置的状态 在每个示例之间,whilebefore(:al

我在做《铁路史》,现在是第11章

为什么会出现这种错误

警告:让声明
在(:all)
钩住:
/用户/xxx/Documents/rails\u projects/sample\u app\u 2/spec/requests/microspost\u pages\u spec.rb:49:in `'中的块(4层)'

这是RSpec 3中不支持的不推荐行为

let
subject
声明不打算在
before(:all)
hook,因为它们用于定义重置的状态 在每个示例之间,while
before(:all)
用于定义 在示例组中的示例之间共享。警告:让 声明另一个用户在(:all)之前的
中访问
/用户/xxx/Documents/rails\u projects/sample\u app\u 2/spec/requests/microspost\u pages\u spec.rb:49:in
`'中的块(4层)'


我的档案在这里

出现错误是因为您有以下代码:

let(:another_user) { FactoryGirl.create(:user) }
before(:all) do
  10.times { FactoryGirl.create(:micropost, user: another_user, content: "Foooo") }
end
其中您的
before(:all)
代码使用了
另一个用户
变量,该变量由
let
定义

您可以通过将(:all)
调用之前的
更改为:

before(:all) do
  user = FactoryGirl.create(:user)
  10.times { FactoryGirl.create(:micropost, user: user, content: "Foooo") }
end

请注意,railstutorial.org上当前定义的教程不包含任何违反限制的代码。

在我的环境中,以下代码有效

  describe "delete links" do
    before(:all) do
      @another_user = FactoryGirl.create(:user)
      10.times { FactoryGirl.create(:micropost, user: @another_user, content: "Foooo") }
    end

    after(:all)  do
      Micropost.delete_all
      User.delete_all
    end

    before { visit user_path(@another_user) }

    it "should not create delete link for not current user" do
      Micropost.paginate(page: 1).each do |mp|
        should_not have_link("delete", href: micropost_path(mp))
      end
    end

    it { should have_content(@another_user.name) }
    it { should have_content("Foooo") }
  end