我在测试create
控制器方法时遇到问题。我的测试:
describe "POST #create" do
it "creates a new admin_user" do
expect{
post :create, :admin_user => FactoryGirl.attributes_for(:admin_user)
}.to change(Admin::User,:count).by(1)
end
end
我得到的失败规范:
1) Admin::UsersController logged in POST #create creates a new admin_user
Failure/Error: expect{
count should have been changed by 1, but was changed by 0
# ./spec/controllers/admin_users_controller_spec.rb:75:in `block (4 levels) in <top (required)>'
这是我的控制器:
def create
@admin_user = Admin::User.new(params[:admin_user])
if @admin_user.save
render nothing: true
end
end
和工厂:
require "factory_girl"
require 'ffaker'
FactoryGirl.define do
factory :admin_user, class: "Admin::User" do |f|
f.name {Faker::Name.first_name}
f.email {Faker::Internet.email}
f.password "aaa"
end
end
和我的模型:
class Admin::User < ActiveRecord::Base
has_secure_password
validates :email, :uniqueness => true, :presence => true
validates_presence_of :password, :on => :create
end
我不知道可能出了什么问题。我已经搜索了整个互联网,但没有找到答案。从 rails 控制台添加用户就可以了。任何帮助将不胜感激。提前致谢。