У меня эти странные ошибки (ниже это всего лишь пример), и я не знаю, что с ним делать.Не удалось найти поле "E-mail" Rspec
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 => "[email protected]"
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" %>
мой взгляд SignUp:
<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
попробуйте изменить 'fill_in 'Email" 'to' fill_in "email" '??? capybara смехотворно педантичен о случае –
@Taryn East Извините, просто забыл изменить это до публикации. В моих файлах все: адрес электронной почты в нижнем регистре. –