我在我的应用上做了一些控制器测试,当我测试用于创建新服务的创建操作时,它给了我这个错误:"预期#count已经改变了1,但是改为0"
Theres my spec:
before do
@user = FactoryGirl.create(:user)
sign_in @user
end
describe "POST #create" do
context "with valid params" do
it "creates a new Service" do
expect {
post :create, params: { user_id: @user, service: FactoryGirl.attributes_for(:service) }
}.to change(Service, :count).by(1)
end
end
end
我的工厂:
FactoryGirl.define do
factory :service do
name "Service Test"
price 1
time 15
quantity 2
user
end
end
我的模特:
class Service
include Mongoid::Document
embedded_in :user
field :name, type: String
field :price, type: Float
field :time, type: Integer
field :quantity, type: Integer
validates :name, :price, :time, :quantity, presence: true
end
控制器:
def create
@service = Service.new(service_params)
@service.user = current_user
if @service.save
redirect_to services_path
else
redirect_to "/services"
end
end
错误:
Failure/Error:
expect {
post :create, params: { user_id: @user, service: FactoryGirl.attributes_for(:service) }
}.to change(Service, :count).by(1)
expected #count to have changed by 1, but was changed by 0
我正在使用mongoid,顺便说一句。服务嵌入在用户中。