2012-12-21 5 views
0

J'essaie d'utiliser l'utilisateur généré par usine fille dans mes rails app. L'objectif est de se connecter et vérifier sa page d'édition, mais ça ne fonctionne pas. Ce que je veux esterreur de test avec rspec et usine fille

<top (required)>': undefined local variable or method `user' for #<Class:0x000001034caaa0> (NameError) 

Voici le code qui » sont exécutées variables

describe "edit" do 
    let(:user) { FactoryGirl.create(:user) } 

    describe "page" do 
    # signin method in utilities 
    before { visit signin_path } 
    fill_in "Email",  with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in"   
    # end signin method in utilities 
    before { visit edit_user_path(user) } 

    it { should have_selector('h1', text: "Update your profile") } 
    it { should have_selector('title', text: "Edit user") } 
    it { should have_link('change', href: 'htttp://gravatar.com/emails') } 
    end 

    describe "with invalid information" do 
     before { visit edit_user_path(user) } 
     before { click_button "Save changes"} 

     it { should have_content('error') } 

    end 



     describe "with valid information" do 
     before { visit edit_user_path(user) } 
     let(:new_name) { "New Name" } 
     let(:new_email) { "[email protected]" } 
     before do 
      fill_in "Name",      with: new_name 
      fill_in "Email",     with: new_email 
      fill_in "Password",     with: user.password 
      fill_in "Confirm Password",   with: user.password 

      click_button "Save changes" 
     end 

     it { should have_selector('title', text: new_name) } 
     it { should have_link('Sign out', href: signout_path) } 
     it { should have_selector('div.alert.alert-success') } 
     specify { user.reload.name.should == new_name } 
     specify { user.reload.email.should == new_email } 
    end 
    end 

Répondre

1

Votre user ne peut pas être utilisé directement dans le bloc describe. Utilisez-le à l'intérieur des blocs it, let, before ou subject (ou quelques autres). Dans ce cas, je pense que vous vouliez dire à l'intérieur d'un bloc before.

Tournez ceci:

describe "page" do 
# signin method in utilities 
before { visit signin_path } 
fill_in "Email",  with: user.email 
fill_in "Password", with: user.password 
click_button "Sign in"   
# end signin method in utilities 
before { visit edit_user_path(user) } 

it { should have_selector('h1', text: "Update your profile") } 
it { should have_selector('title', text: "Edit user") } 
it { should have_link('change', href: 'htttp://gravatar.com/emails') } 
end 

Dans ceci:

describe "page" do 
    before do 
    visit signin_path 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in"   
    visit edit_user_path(user) 
    end 

    it { should have_selector('h1', text: "Update your profile") } 
    it { should have_selector('title', text: "Edit user") } 
    it { should have_link('change', href: 'htttp://gravatar.com/emails') } 
end 
+0

merci, il a travaillé. – skip87

+0

@ skip87 Vous êtes les bienvenus. Je fais la même chose par accident de temps en temps, aussi. – nicholaides

Questions connexes