2012-11-28 52 views
1

我一直在研究Michael Hartl的Rails教程應用程序,以基本上製作twitter的副本。我在測試套件中遇到了一個問題。我的代碼和Hartl的代碼之間的一個區別是,我不想安裝FactoryGirl來生成測試用戶,所以我在測試開始時創建了一個用戶。我覺得這可能是問題的一部分,但想知道究竟是什麼錯誤。我的測試腳本如下(我得到的錯誤在該行:FILL_IN 「電子郵件」,有:user.email):rspec錯誤未定義的局部變量或方法'user'michael hartl教程

describe "Authentication" do 
subject {page} 
before do 
    @user = User.create(name: "Example User", email: "[email protected]", 
      password: "foobar", password_confirmation: "foobar") 
end 

describe "signin page" do 
    before { visit signin_path } 

    describe "with invalid information" do 
     before { click_button "Sign in"} 

     it { should have_selector('title', text: 'Sign in')} 
     it { should have_selector('div.alert.alert-error', text: 'Invalid')} 

    end 



    describe "with valid information" do 
     let(:user) { User.find_by_email(@user.email) } 

     fill_in "Email", with: user.email 
     fill_in "Password", with: user.password 
     click_button "Sign in" 
    end 

    it { should have_selector('title', text: user.name) } 
    it { should have_link('Profile', href: user_path(user)) } 
    it { should have_link('Sign out', href: signout_path) } 
    it { should_not have_link('Sign in', href: signin_path) } 
    it { should have_link('Sign out')} 


    describe "after visiting another page" do 
     before { click_link "Home" } 
     it { should_not have_selector('div.alert.alert-error') } 
    end 
end 

UPDATE:託管解決我自己 - 我最終通過在有效信息塊中創建用戶實例來解決問題,然後直接引用我創建的實例而不是使用let(:user)方法。

'使用有效信息'描述「在@user = User.create(名稱:」示例用戶「,電子郵件:」[email protected]「,密碼:」foobar「,password_confirmation:」foobar「

 fill_in "Email", with: @user.email 
     fill_in "Password", with: @user.password 
     click_button "Sign in" 
    end 


    it { should have_selector('title', text: @user.name) } 
    it { should have_link('Profile', href: user_path(@user)) } 
    it { should have_link('Sign out', href: signout_path) } 
    it { should_not have_link('Sign in', href: signin_path) } 
    it { should have_link('Sign out')} 
end' 

回答

1

,我認爲你應該做的就是:

describe "Authentication" do 
    subject { page } 

    let!(:user) do 
    User.create(name: "Example User", email: "[email protected]", 
     password: "foobar", password_confirmation: "foobar") 
    end 

    describe "signin page" do 
    before { visit signin_path } 

    describe "with invalid information" do 
     before { click_button "Sign in"} 

     it { should have_selector('title', text: 'Sign in')} 
     it { should have_selector('div.alert.alert-error', text: 'Invalid')} 
    end 

    describe "with valid information" do 
     fill_in "Email", with: user.email 
     fill_in "Password", with: user.password 
     click_button "Sign in" 
    end 

    it { should have_selector('title', text: user.name) } 
    it { should have_link('Profile', href: user_path(user)) } 
    it { should have_link('Sign out', href: signout_path) } 
    it { should_not have_link('Sign in', href: signin_path) } 
    it { should have_link('Sign out')} 

    describe "after visiting another page" do 
     before { click_link "Home" } 

     it { should_not have_selector('div.alert.alert-error') } 
    end 
    end 
end 

什麼let!做它實例化user訪問,付諸before塊和重置每個實例之後。

2

您沒有將此行包裝到it塊。 例如

it "can sign up" do 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
end 
相關問題