2014-01-14 41 views
0

我在做railstutorial,現在是第11章。railstutorial,這是RSpec 3不支持的棄用行爲

爲什麼這個錯誤?

警告:讓聲明another_userbefore(:all) 鉤在訪問:
/Users/xxx/Documents/rails_projects/sample_app_2/spec/requests/micropost_pages_spec.rb:49:in `塊(4級)在「

這已被棄用,不會在RSpec的支持行爲3.

letsubject聲明不打算在 被稱爲3210鉤子,因爲它們存在以定義在每個示例之間重置 的狀態,而存在before(:all)以定義在示例組中的示例之間共享的狀態 。警告:讓 聲明another_userbefore(:all)鉤在訪問:在'


/Users/xxx/Documents/rails_projects/sample_app_2/spec/requests/micropost_pages_spec.rb:49:in `塊(4級)

我的檔案就在這裏。 enter link description here

回答

1

你得到的錯誤,因爲你有下面的代碼:

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

在您before(:all)代碼使用another_user可變的,由let定義。

可以消除通過更改before(:all)調用警告:

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

注意,因爲目前railstutorial.org定義的教程不包括違反限制的任何代碼。

0

在我的環境中,以下代碼工作。

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 
相關問題