ruby-on-rails - 无法找到字段 "Email"Rspec

标签 ruby-on-rails ruby rspec capybara

我遇到了这些奇怪的错误(下面只是一个例子),我不知道该怎么办。

4) Hotels Show hotel shows comment of the user
 Failure/Error: sign_in hotel.user
 Capybara::ElementNotFound:
   Unable to find field "email"
 # ./spec/support/utilities.rb:11:in `sign_in'
 # ./spec/requests/hotels_spec.rb:10:in `block (2 levels) in <top (required)>'

这是我的utilities.rb 文件:

    include ApplicationHelper

def sign_in(user, options={})
    visit new_user_session_path
    fill_in "Email",    :with => user.email
    fill_in "Password", :with => user.password
    click_button "Sign in"
end

def sign_up_as_admin(user)
  visit new_user_registration_path
  fill_in "Name",    :with => "Admin"
  fill_in "Email",    :with => "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f6f3fafef9d7f6f3fafef9b9f4f8fa" rel="noreferrer noopener nofollow">[email protected]</a>"
  fill_in "Password", :with => "user.password"
  fill_in "password_confirmation", :with => "user.password"
  check("admin")
  click_button "Sign up"
end

RSpec::Matchers.define :have_error_message do |message|
  match do |page|
    expect(page).to have_selector('div.alert.alert-error', text: message)
  end
end

我的登录 View :

    <h2>Log in</h2>

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, id: "Email" %>
  </div>

  <div class="field">
    <%= f.label :password %><br />
    <%= f.password_field :password, autocomplete: "off", id: "Password" %>
  </div>

  <% if devise_mapping.rememberable? -%>
    <div class="field">
      <%= f.check_box :remember_me %>
      <%= f.label :remember_me %>
    </div>
  <% end -%>

  <div class="actions">
    <%= f.submit "Sign in" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

我的注册 View :

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name, autofocus: true %>
  </div>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email %>
  </div>

  <div class="field">
    <%= f.label :password %>
    <% if @validatable %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off", id: "password_confirmation" %>
  </div>

  <div class="checkbox">
  <%= f.label "Wanna be admin?" %>
  <%= f.check_box :admin, :id => "admin" %>
  </div>
  <br>
  <br>

  <div class="actions">
    <%= f.submit "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

我不知道是什么导致了这些错误,因为我使用的是小写:电子邮件。有趣的是,第二种方法sign_up_as_admin工作正常。 任何想法都会有帮助。谢谢!

更新 1: 对不起大家。我刚刚在 before(:each) block 中做了两次 sign_in hotel.user 。看起来再多一个就足够了:) 我的 requests/hotels_spec.rb 文件:

require 'spec_helper'

describe "Hotels" do

  let(:hotel) { FactoryGirl.create(:hotel) }
  let(:random_guy) { FactoryGirl.create(:user) }
  let(:comment) { FactoryGirl.create(:comment) }

  before(:each) do       *# Here the first time.*
      sign_in hotel.user
      visit hotels_path
    end

    it {expect(page).to have_content(hotel.name)}
    it {expect(page).to have_content(hotel.price_for_room)}
    it {expect(page).to have_content(hotel.star_rating)}
    it {expect(page).to have_content(hotel.average_rating)}
    it {expect(page).to have_link("Edit")}
    it {expect(page).to have_link("Delete")}

  describe "edit hotel" do

    it "visit edit hotel page" do
      visit edit_hotel_path(hotel)
      expect(page).to have_link("Edit hotel")
      expect(page).to have_link("Delete hotel")
      expect(page).to have_content "Editing hotel"
    end 

    it "submit hotel" do
      visit edit_hotel_path(hotel)
      fill_in 'name', with: "funny stuff :)"
      click_button "Submit"   
      expect(page).to have_content("funny stuff :)")
    end  
  end

  describe "add a new hotel" do
    it "visit new hotel page" do
      visit new_hotel_path
      expect(page).to have_content "New hotel"
    end

    it "create new hotel" do
      visit new_hotel_path
      fill_in "name",          with: "qwerty"
      fill_in "price", with: 123
      fill_in "star_rating", with: 1
      expect { click_button "Submit" }.to change(Hotel,:count).by(1)
    end
  end

  describe "Show hotel" do
    before :each do
      sign_in hotel.user      *# And here the second time. That was cousing these errors*
      visit hotel_path(hotel)
    end

    it "shows all the info about the hotel on the show page" do
      expect(page).to have_content (hotel.name)
      expect(page).to have_content(hotel.price_for_room)
      expect(page).to have_content(hotel.star_rating)
      expect(page).to have_content(hotel.average_rating)
      expect(page).to have_content(hotel.room_description)
      expect(page).to have_content(hotel.address.country)
      expect(page).to have_content(hotel.address.state)
      expect(page).to have_content(hotel.address.city)
      expect(page).to have_content(hotel.address.street)
    end 

    it "creates a comment successfully with valid information" do
      fill_in 'body', with: "Test message"
      expect { click_button "Submit" }.to change(Comment, :count).by(1)
      expect(page).to have_selector("textarea", visible: "Test message")
    end 

    it "does not create a comment with invalid information" do
      fill_in 'body', with: ""
      expect { click_button "Submit" }.to change(Comment, :count).by(0)
      expect(page).to have_content("Body can't be blank")
    end

    it "shows comment of the user" do
      visit hotel_path hotel
      fill_in 'body', with: "comment1"
      click_button "Submit"
      expect(page).to have_content("comment1")
    end  

    it "delete hotel if you added this hotel " do
      expect { click_link "Delete hotel" }.to change(Hotel,:count).by(-1)
    end 

    it "random guy shouldnt see Delete button" do
      click_link "Sign out"
      sign_in random_guy
      visit hotel_path(hotel)
      expect(page).not_to have_selector("button", visible: "Delete hotel")
    end
  end
end

最佳答案

当您使用 form_for 帮助程序时,字段会获得 model_attribute 的 id,因此如果您的设计模型是 user,则它将是 user_email。

尝试将其链接到

fill_in "user_email", :with => user.email

或者,您可以为电子邮件字段提供您想要的显式 ID,例如

<%= f.email_field :email, id: "email" %>

关于ruby-on-rails - 无法找到字段 "Email"Rspec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30521170/

相关文章:

ruby-on-rails - 使 rails 中的数组仅显示数字

ruby - 如何在 Ruby 中替换字符串中每个区分大小写的单词实例?

ruby-on-rails - 算法 ruby​​ (rails) 最佳分数数组

ruby-on-rails - 使用 open() 方法在 Rails 中创建/重命名临时文件

ruby-on-rails - 在 minitest 中单步执行代码

ruby - 在测试之间删除 Cassandra DB (Rspec)

ruby-on-rails - NoMethodError:#<RSpec::ExampleGroups::CompetitionsController::Create> 的未定义方法 `mock'

ruby-on-rails - Rails 不为开发模式下的 Assets 提供服务

ruby-on-rails - 良好实践——模型中的重定向? -- rails 3.1

ruby-on-rails - Jruby on Rails 使用 Tomcat 进行日志记录