Rails教程无法在10.34之后的规范运行中找到字段“名称”

时间:2014-03-07 19:04:39

标签: ruby-on-rails ruby rspec ruby-on-rails-4

我正在浏览Rails教程,我遇到的问题似乎导致很多其他测试失败。有23个错误,其中大多数都解释说有一个名为“Name”的缺失字段。当我运行命令

bundle exec rspec spec/requests/user_pages_spec.rb

我收到以下错误(每次运行时都会改变顺序):

  

1)用有效信息编辑用户页面        失败/错误:fill_in“名称”,带:new_name        水豚:: ElementNotFound:          无法找到字段“名称”        './spec/requests/user_pages_spec.rb:136:in'块(4级)in'

结果,许多其他案件似乎都失败了。什么应用程序文件会导致此问题? sessions_helper.rb?


这是我的spec / requests / user_pages_spec.rb:require'pec_helper'

describe "User pages" do

  subject { page }

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

    before do
      sign_in user
      visit users_path
    end

    it { should have_title('All users') }
    it { should have_content('All users') }

    describe "pagination" do

      before(:all) { 30.times { FactoryGirl.create(:user) } }
      after(:all)  { User.delete_all }

      it { should have_selector('div.pagination') }

      it "should list each user" do
        User.paginate(page: 1).each do |user|
          expect(page).to have_selector('li', text: user.name)
        end
      end
    end

    describe "delete links" do

      it { should_not have_link('delete') }

      describe "as an admin user" do
        let(:admin) { FactoryGirl.create(:admin) }
        before do
          sign_in admin
          visit users_path
        end

        it { should have_link('delete', href: user_path(User.first)) }
        it "should be able to delete another user" do
          expect do
            click_link('delete', match: :first)
          end.to change(User, :count).by(-1)
        end
        it { should_not have_link('delete', href: user_path(admin)) }
      end
    end
  end

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
    let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }

    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }

    describe "microposts" do
      it { should have_content(m1.content) }
      it { should have_content(m2.content) }
      it { should have_content(user.microposts.count) }
    end
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title(full_title('Sign up')) }
  end

  describe "signup" do

    before { visit signup_path }

    let(:submit) { "Create my account" }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end

      describe "after submission" do
        before { click_button submit }
        signup_errors
      end
    end

    describe "with valid information" do

      before { valid_signup }

      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
      end

      describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by(email: 'user@example.com') }

        it { should have_link('Sign out') }
        it { should have_title(user.name) }
        it { should have_welcome_message( 'Welcome')}

        describe "followed by signout" do
          before { click_link "Sign out" }
          it { should have_link('Sign in') }
        end
      end
    end
  end

  describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before do
      sign_in user
      visit edit_user_path(user)
    end

    describe "page" do
      it { should have_content("Update your profile") }
      it { should have_title("Edit user") }
      it { should have_link('change', href: 'http://gravatar.com/emails') }
    end

    describe "with valid information" do
      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_title(new_name) }
      it { should have_selector('div.alert.alert-success') }
      it { should have_link('Sign out', href: signout_path) }
      specify { expect(user.reload.name).to  eq new_name }
      specify { expect(user.reload.email).to eq new_email }
    end
  end
end

1 个答案:

答案 0 :(得分:1)

运行规范时收到的错误表明Capybara需要填写一个名为"Name"的字段(因为您要求它在第fill_in "Name", with: new_name行)。

如果出于某种原因,在登录后您没有进入用户表单,或者用户表单缺少"Name"字段 - 这可以解释您的失败。

在你的comment中,你说在注册后你没有表格,所以这肯定会解释。

要验证这一点,您可以将测试添加到describe "page",如下所示:

it { should have_field("Name") }

如果失败了 - 你不能指望任何指望该字段的测试通过......