运行测试时遇到这些错误:
1) UsersController PUT 'update' authentication of edit/update pages for non-signed-in users should deny access to 'edit'
Failure/Error: @user = Factory(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/controllers/users_controller_spec.rb:130
2) UsersController PUT 'update' authentication of edit/update pages for non-signed-in users should deny access to 'update'
Failure/Error: @user = Factory(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/controllers/users_controller_spec.rb:130
3) UsersController PUT 'update' authentication of edit/update pages for signed-in users should require matching users for 'edit'
Failure/Error: @user = Factory(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/controllers/users_controller_spec.rb:130
4) UsersController PUT 'update' authentication of edit/update pages for signed-in users should require matching users for 'update'
Failure/Error: @user = Factory(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/controllers/users_controller_spec.rb:130
5) UsersController PUT 'update' GET 'edit' should be successful
Failure/Error: @user = Factory(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/controllers/users_controller_spec.rb:168
6) UsersController PUT 'update' GET 'edit' should have a link to change the Gravatar
Failure/Error: @user = Factory(:user)
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/controllers/users_controller_spec.rb:168
它似乎不喜欢这一行:
before(:each) do
@user = Factory(:user)
test_sign_in(@user)
end
工厂代码
# By using the symbol ':user', we get Factory Girl to simulate the User model.
Factory.define :user do |user|
user.name "Insert Name"
user.email "email@example.com"
user.password "password"
user.password_confirmation "password"
end
我认为这就是你想要的。
用户控制器规范
require 'spec_helper'
describe UsersController do
render_views
describe "GET 'show'" do
before(:each) do
@user = Factory(:user)
end
it "should be successful" do
get :show, :id => @user
response.should be_success
end
it "should find the right user" do
get :show, :id => @user
assigns(:user).should == @user
end
it "should include the user's name" do
get :show, :id => @user
response.should have_selector("h1", :content => @user.name)
end
it "should have a profile image" do
get :show, :id => @user
response.should have_selector("h1>img", :class => "gravatar")
end
end
describe "GET 'new'" do
it "should be successful" do
get :new
response.should be_success
end
end
describe "POST 'create'" do
describe "failure" do
before(:each) do
@attr = { :name => "", :email => "", :password => "", :password_confirmation => "" }
end
it "should not create a user" do
lambda do
post :create, :user => @attr
end.should_not change(User, :count)
end
it "should render the 'new' page" do
post :create, :user => @attr
response.should render_template('new')
end
end
describe "success" do
before(:each) do
@attr = { :name => "New User", :email => "user@example.com", :password => "password", :password_confirmation => "password" }
end
it "should create a user" do
lambda do
post :create, :user => @attr
end.should change(User, :count).by(1)
end
it "should have a welcome message" do
post :create, :user => @attr
flash[:success].should =~ /Welcome to InTouch/i
end
it "should sign the user in" do
post :create, :user => @attr
controller.should be_signed_in
end
end
end
describe "PUT 'update'" do
before(:each) do
@user = Factory(:user)
test_sign_in(@user)
end
describe "failure" do
before(:each) do
@attr = { :email => "", :name => "", :password => "", :password_cofirmation => "" }
end
it "should render the 'edit' page" do
put :update, :id => @user, :user => @attr
response.should render_template('edit')
end
describe "success" do
before(:each) do
@attr = { :name => "New Name", :email => "user@example.org", :password => "newpassword", :password_confirmation => "newpassword" }
end
it "should change the user's attributes" do
put :update, :id => @user, :user => @attr
@user.reload
@user.name.should == @attr[:name]
@user.email.should == @attr[:email]
end
it "should redirect to the user show page" do
put :update, :id => @user, :user => @attr
response.should redirect_to(user_path(@user))
end
it "should have a flash message" do
put :update, :id => @user, :user => @attr
flash[:success].should =~ /updated/
end
end
end
describe "authentication of edit/update pages" do
before(:each) do
@user = Factory(:user)
end
describe "for non-signed-in users" do
it "should deny access to 'edit'" do
get :edit, :id => @user
response.should redirect_to(signin_path)
end
it "should deny access to 'update'" do
put :update, :id => @user, :user => {}
response.should redirect_to(signin_path)
end
end
describe "for signed-in users" do
before(:each) do
wrong_user = Factory(:user, :email => "user@example.net")
test_sign_in(wrong_user)
end
it "should require matching users for 'edit'" do
get :edit, :id => @user
response.should redirect_to(root_path)
end
it "should require matching users for 'update'" do
put :update, :id => @user, :user => {}
response.should redirect_to(root_path)
end
end
end
describe "GET 'edit'" do
before(:each) do
@user = Factory(:user)
test_sign_in(@user)
end
it "should be successful" do
get :edit, :if => @user
response.should be_success
end
it "should have a link to change the Gravatar" do
get :edit, :id => @user
gravatar_url = "http://gravatar.com/emails"
response.should have_selector("a", :href => gravatar_url, :content => "change")
end
end
end
end
答案 0 :(得分:3)
向我们展示用户工厂代码。我猜你为每个用户使用非唯一的电子邮件。尝试在Factory序列的帮助下为用户创建电子邮件。另外,检查重复记录上的_test db。