跳过创建时的回调 Rails
Skip callback on create Rails
我想为 Rspec 测试创建一个 Active Record 模型。
不过,这个模型有回调,即:before_create和after_create方法(我觉得这些方法叫回调不验证如果我没记错的话)。
有没有办法在不触发回调的情况下创建对象?
我以前尝试过的一些解决方案/对我的情况不起作用:
更新方法:
update_column等更新方法将不起作用,因为我想创建一个对象,当对象不存在时我无法使用更新方法。
工厂女工和改造后:
FactoryGirl.define do
factory :withdrawal_request, class: 'WithdrawalRequest' do
...
after(:build) { WithdrawalRequest.class.skip_callback(:before_create) }
end
end
Failure/Error: after(:build) { WithdrawalRequest.class.skip_callback(:before_create) }
没有方法错误:
Class:Class
的未定义方法 `skip_callback'
Skip callbacks on Factory Girl and Rspec
跳过回调
WithdrawalRequest.skip_callback(:before_create)
withdrawal_request = WithdrawalRequest.create(withdrawal_params)
WithdrawalRequest.set_callback(:before_create)
Failure/Error: WithdrawalRequest.skip_callback(:before_create)
NoMethodError:#
的未定义方法“_before_create_callbacks”
How to save a model without running callbacks in Rails
我也试过了
WithdrawalRequest.skip_callbacks = true
哪个也不行。
------------编辑------------
我的工厂函数被编辑为:
after(:build) { WithdrawalRequest.skip_callback(:create, :before, :before_create) }
我的 before_create 函数如下所示:
class WithdrawalRequest < ActiveRecord::Base
...
before_create do
...
end
end
------------ 编辑 2 ----------
我将 before_create 更改为一个函数以便我可以引用它。这些都是更好的做法吗?
class WithdrawalRequest < ActiveRecord::Base
before_create :before_create
...
def before_create
...
end
end
根据参考答案:
FactoryGirl.define do
factory :withdrawal_request, class: 'WithdrawalRequest' do
...
after(:build) { WithdrawalRequest.skip_callback(:create, :before, :callback_to_be_skipped) }
#you were getting the errors here initially because you were calling the method on Class, the superclass of WithdrawalRequest
#OR
after(:build) {|withdrawal_request| withdrawal_request.class.skip_callback(:create, :before, :callback_to_be_skipped)}
end
end
我想为 Rspec 测试创建一个 Active Record 模型。
不过,这个模型有回调,即:before_create和after_create方法(我觉得这些方法叫回调不验证如果我没记错的话)。
有没有办法在不触发回调的情况下创建对象?
我以前尝试过的一些解决方案/对我的情况不起作用:
更新方法:
update_column等更新方法将不起作用,因为我想创建一个对象,当对象不存在时我无法使用更新方法。
工厂女工和改造后:
FactoryGirl.define do
factory :withdrawal_request, class: 'WithdrawalRequest' do
...
after(:build) { WithdrawalRequest.class.skip_callback(:before_create) }
end
end
Failure/Error: after(:build) { WithdrawalRequest.class.skip_callback(:before_create) }
没有方法错误: Class:Class
的未定义方法 `skip_callback'Skip callbacks on Factory Girl and Rspec
跳过回调
WithdrawalRequest.skip_callback(:before_create)
withdrawal_request = WithdrawalRequest.create(withdrawal_params)
WithdrawalRequest.set_callback(:before_create)
Failure/Error: WithdrawalRequest.skip_callback(:before_create)
NoMethodError:#
的未定义方法“_before_create_callbacks”How to save a model without running callbacks in Rails
我也试过了
WithdrawalRequest.skip_callbacks = true
哪个也不行。
------------编辑------------
我的工厂函数被编辑为:
after(:build) { WithdrawalRequest.skip_callback(:create, :before, :before_create) }
我的 before_create 函数如下所示:
class WithdrawalRequest < ActiveRecord::Base
...
before_create do
...
end
end
------------ 编辑 2 ----------
我将 before_create 更改为一个函数以便我可以引用它。这些都是更好的做法吗?
class WithdrawalRequest < ActiveRecord::Base
before_create :before_create
...
def before_create
...
end
end
根据参考答案:
FactoryGirl.define do
factory :withdrawal_request, class: 'WithdrawalRequest' do
...
after(:build) { WithdrawalRequest.skip_callback(:create, :before, :callback_to_be_skipped) }
#you were getting the errors here initially because you were calling the method on Class, the superclass of WithdrawalRequest
#OR
after(:build) {|withdrawal_request| withdrawal_request.class.skip_callback(:create, :before, :callback_to_be_skipped)}
end
end