1

我正在关注 railstutorial.org 教程我收到以下错误

  1) User pages signup with valid information should create a user
     Failure/Error: fill_in "password" , with: "foobar"
     Capybara::ElementNotFound:
       cannot fill in, no text field, text area or password field with id, name, or label 'password' found
     # (eval):2:in `fill_in'
     # ./spec/requests/user_pages_spec.rb:32:in `block (4 levels) in <top (required)>'

  2) Authentication signin with invalid information 
     Failure/Error: before { click_button "Sign in" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Sign in' found
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'

  3) Authentication signin with invalid information 
     Failure/Error: before { click_button "Sign in" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Sign in' found
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'

  4) Authentication signin with invalid information after visiting another page 
     Failure/Error: before { click_button "Sign in" }
     Capybara::ElementNotFound:
       no button with value or id or text 'Sign in' found
     # (eval):2:in `click_button'
     # ./spec/requests/authentication_pages_spec.rb:14:in `block (4 levels) in <top (required)>'

Finished in 3.67 seconds
46 examples, 4 failures

和我的 authentication_pages_spec.rb

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }
    it { should have_selector('h1', text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  describe "signin" 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') }
      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end


    let(:user) { FactoryGirl.create(:user) }
    before 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)}
  end
end 

和 app/view/sessions/new.html.erb

<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>
      <%= f.label :email %>
      <%= f.text_field :email %>
      <%= f.label :password %>
      <%= f.password_field :password %>
      <%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
    <% end %>
    <p>New user? <%= link_to "Sign up now!", signup_path %></p>
  </div>
</div>
4

2 回答 2

5

您缺少描述块。使您的代码如下所示:

require 'spec_helper'

describe "Authentication" do
  subject { page }

  describe "signin page" do
    before { visit signin_path }
    it { should have_selector('h1', text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  describe "signin" 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') }
      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
      end
    end

    # The following block is missing
    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before 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)}
     end
  end
end

Capybara 通常visit会先让你使用它的方法,然后才会让你使用它的fill_in方法。您忘记了将最终before块包装在一个describe块中,就像我在上面所做的那样,因此 before 块中的代码实际上覆盖了使用 visit 方法的前一个 before 块。所以这就是你有这个问题的原因。通过将最后一组断言包装在描述块中,您可以解决问题。

于 2013-01-07T14:43:44.837 回答
0

看起来事情在 1) 中开始出错,我相信这实际上是在 spec/requests/user_pages_spec.rb 文件中处理的。你能张贴你在那里的东西吗?此外,根据您在教程中的位置,您可能会丢失一些 authentication_pages_spec.rb 文件,因为http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec-下的清单 8.6 signin_tests有工厂女孩调用和以下由描述块包围的“it”子句。

于 2013-01-02T01:05:32.697 回答