1

我一直在研究 Michael Hartl 的 Rails 教程应用程序,以制作 Twitter 的复制品。我在我的测试套件中遇到了问题。我的代码和 Hartl 的代码之间的一个区别是我不想安装 FactoryGirl 来生成测试用户,所以我在测试开始时创建了一个用户。我觉得这可能是问题的一部分,但想了解究竟出了什么问题。我在下面的测试脚本(我在以下行收到错误:fill_in“Email”,带有: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

结尾

更新:设法自行解决 - 我最终通过使用有效信息块在描述中创建用户实例来解决,然后直接引用我创建的实例而不是使用 let(:user) 方法。

'describe "with valid information" do before do @user = User.create(name: "Example User", email: "user@example.com", password: "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'
4

2 回答 2

2

你没有包装这条线来it阻止。例如

 it "can sign up" do
   fill_in "Email", with: user.email
   fill_in "Password", with: user.password
   click_button "Sign in"
 end
于 2012-11-28T02:35:31.567 回答
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块并重置。

于 2012-11-29T08:49:20.387 回答