无法找到字段“电子邮件”Rspec

时间:2015-05-29 04:30:45

标签: 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 => "admin@admin.com"
  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

我的登录视图:

    <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" %>

我的注册视图:

<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: 对不起大家。我刚刚在sign_in hotel.user块中加倍before(:each)。看起来还不够:)我的要求/ 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

3 个答案:

答案 0 :(得分:2)

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

尝试将其加入

finally

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

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

答案 1 :(得分:1)

将utilities.rb中的第11行更改为:

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

答案 2 :(得分:0)

对不起伙计们。我在sign_in hotel.user块中加倍了before(:each)。更新了我的帖子,详细信息。