我想通过设计对用户进行身份验证,然后访问他们的帐户页面并更改密码。
describe Users::UsersController do
describe "abc" do
it "abc" do
user = FactoryGirl.create :user
sign_in :user, user
visit edit_user_registration_path
save_and_open_page # -- user isn't authenticated yet!
fill_in # .......
end
end
但是,此测试效果很好
it "should sign in" do
user = FactoryGirl.create :user
sign_in :user, user
subject.current_user.should_not be_nil
end
答案 0 :(得分:0)
我所做的是 - 在spec / support / utilities.rb中创建了一个文件 和我的utilities.rb有这个
def sign_in(user)
visit new_user_session_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
在spec / requests / authentication_pages_spec.rb(由我创建)之后,确实以这种方式设计了身份验证
require 'spec_helper'
describe 'UserPages' do
subject { page }
describe "allow registered user to sign in successfully" do
before do
user = FactoryGirl.create(:user)
sign_in user
end
it { should have_link("Logout") }
it { should_not have_link("Login") }
end
describe "edit" do
before do
@user = FactoryGirl.create(:user)
sign_in @user
visit edit_user_registration_path
end
describe "with valid information" do
let!(:new_name) { "New Name" }
let!(:new_password) { "newpassword" }
before do
fill_in "Name", with: new_name
fill_in "Email", with: @user.email
fill_in "user[password]", with: new_password
fill_in "user[password_confirmation]", with: new_password
click_button "Update"
end
specify { @user.reload.name.should == new_name }
end
end
end
和我的spec / factories.rb有这个
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "Person_#{n}" }
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar123"
password_confirmation "foobar123"
end
end