以下是用于在用户中登录帐户的2个规范:
it "signs user in with valid email/password combination" do
user = User.create!(user_attributes)
visit root_url
click_link "Sign In"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_link "Sign In"
expect(current_path).to eq(user_path(user))
expect(page).to have_text("Welcome back, #{user.first_name}!")
expect(page).to have_link(user.name)
expect(page).not_to have_link('Sign In')
expect(page).not_to have_link('Sign Up')
end
it "does not sign in invalid email/password combo" do
user = User.create!(user_attributes)
visit root_url
click_link "Sign In"
fill_in "Email", with: user.email
fill_in "Password", with: "no match"
click_link "Sign In"
expect(page).to have_text("Invalid email/password combination!")
expect(page).not_to have_link(user.name)
expect(page).to have_link('Sign In')
expect(page).to have_link('Sign Up')
end
我收到了以下无法解决的错误消息:
1) signing a user in signs user in with valid email/password combination
Failure/Error: expect(current_path).to eq(user_path(user))
expected: "/users/1"
got: "/session/new"
(compared using ==)
# ./spec/features/sign_in_spec.rb:26:in `block (2 levels) in <top (required)>'
2) signing a user in does not sign in invalid email/password combo
Failure/Error: expect(page).to have_text("Invalid email/password combination!")
expected to find text "Invalid email/password combination!" in "The Market Sign Up | Sign In | Home About Products Location Register Log In Sign In Email Password www.themarket.com | 312-401-4570 | Beverly | Oak Lawn | Evergreen Park"
# ./spec/features/sign_in_spec.rb:46:in `block (2 levels) in <top (required)>'
这是我的会话控制器:
class SessionsController < ApplicationController
def new
end
def create
if user = User.authenticate(params[:email], params[:password])
session[:user_id] = user.id
flash[:notice] = "Welcome back, #{user.first_name}!"
redirect_to user
else
flash.now[:alert] = "Invalid email/password combination!"
render :new
end
end
端
当我在浏览器中运行它时效果很好,但测试失败了,我不知道为什么。
答案 0 :(得分:0)
您正在使用click_link "Sign In"
而不是提交表单。
visit root_url
click_link "Sign In"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign In"
您可以将规格清理为:
let!(:user){ User.create!(user_attributes) }
def sign_in_with(email, password)
visit root_url
click_link "Sign In"
fill_in "Email", with: email
fill_in "Password", with: password
click_button "Sign In"
end
# prefer scenario over it for feature specs
it "signs user in with valid email/password combination" do
sign_in_with(user.email, user.password)
expect(current_path).to eq(user_path(user))
expect(page).to have_text("Welcome back, #{user.first_name}!")
expect(page).to have_link(user.name)
expect(page).not_to have_link('Sign In')
expect(page).not_to have_link('Sign Up')
end
it "does not sign in invalid email/password combo" do
sign_in_with(user.email, "no match")
expect(page).to have_text("Invalid email/password combination!")
expect(page).not_to have_link(user.name)
expect(page).to have_link('Sign In')
expect(page).to have_link('Sign Up')
end