一直试图弄清楚这一点,但似乎无法变绿。这是我得到的 rspec 错误:
1) UsersController POST create with valid parameters creates a new user
Failure/Error: expect {post :create, user}.to change(User, :count).by 1
count should have been changed by 1, but was changed by 0
# ./spec/controllers/users_controllers_spec.rb:36:in `block (4 levels) in <top
(required)>'
这是我在 users_controllers_spec 中的相关测试:
describe UsersController do
let(:user) {FactoryGirl.create :user}
context 'POST create' do
context 'with valid parameters' do
before {post :create, user}
let(:user) {FactoryGirl.build :user}
it 'creates a new user' do
expect {post :create, user}.to change(User, :count).by 1
end
it {should respond_with 200}
end
这是我的 users_controller 创建方法:
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome #{@user.name.capitalize}! Please complete your profile."
redirect_to profile_path(current_user)
else
render :new
end
end
另外,这是我的 factory.rb 文件:
FactoryGirl.define do
factory :user do
name "John"
email "John@example.com"
password "password"
password_confirmation "password"
end
end
我错过了什么?我觉得这很明显,但我似乎无法弄清楚。任何帮助将非常感激。