rspec错误未定义局部变量或方法'用户'michael hartl教程

时间:2012-11-28 02:25:17

标签: ruby-on-rails rspec railstutorial.org

我一直致力于迈克尔哈特尔的Rails教程应用程序基本上制作推特的复制品。我在测试套件中遇到了问题。我的代码和Hartl代码之间的一个区别是我不想安装FactoryGirl来生成测试用户,因此我在测试开始时创建了一个用户。我觉得这可能是问题的一部分,但我想了解究竟是什么问题。我的测试脚本如下(我收到了错误:fill_in" Email",with:user.email):

describe "Authentication" do 
subject {page}
before do
    @user = User.create(name: "Example User", email: "user@example.com", 
          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

更新:管理我自己解决 - 我最终解决了使用有效信息块在describe中创建用户实例,然后直接引用我创建的实例而不是使用let(:user)方法。

'用有效信息描述""之前做@user = User.create(名称:"示例用户",电子邮件:" user@example.com" ;,密码:" foobar",password_confirmation: " foobar的&#34)

        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'

2 个答案:

答案 0 :(得分:2)

您没有将此行换行到it块。 例如

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

答案 1 :(得分:1)

我认为你应该做的只是:

describe "Authentication" do 
  subject { page }

  let!(:user) do
    User.create(name: "Example User", email: "user@example.com", 
      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阻止并在每个示例后重置。