用rspec和工厂女孩测试错误

时间:2012-12-21 05:14:15

标签: ruby-on-rails ruby-on-rails-3 rspec factory-bot

我正在尝试使用由我的rails应用程序中的工厂女孩​​生成的用户。目标是登录并查看他的编辑页面但它无法正常工作。 我得到的是

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

以下是'执行

的代码
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) { "new@example.com" }
        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

1 个答案:

答案 0 :(得分:1)

您的user变量不能直接在describe块中使用。在itletbeforesubject块(或其他几个)中使用它。在这种情况下,我认为你的意思是它在before块内。

转过来:

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 "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