我的方法:
Shoulda::Context.send(:include, Module.new do
def when_reminder_sent(n, &blk)
context "when reminder #{n} was sent" do
setup { subject.free_trial_notice_sent = n }
blk.bind(self).call
end
end
def when_payment_was_received(&blk)
context 'when payment has been received' do
setup { subject.last_payment_received_at = 1.day.ago }
blk.bind(self).call
end
end
end)
它注入并返回true。所以我知道它在那里。但是当我这样做时:
context 'on day 15' do
when_reminder_sent(15) { should('be nil') { assert_nil subject.reminder_to_send }}
它返回:
NoMethodError: undefined method `when_reminder_sent' for EnterpriseRegistrationTest:Class
啊,所以我看到它试图在我测试的模型上使用这个方法..我怎样才能确保它在原来的Shoulda上下文中使用该方法?
*编辑:修正了一些拼写错误*
dyld: DYLD_ environment variables being ignored because main executable (/bin/ps) is setuid or setgid
/Users/elephanttrip/.rvm/gems/ruby-1.9.3-p125@website/gems/activesupport-3.2.14/lib/active_support/inflector/methods.rb:230: Use RbConfig instead of obsolete and deprecated Config.
/Users/elephanttrip/.rvm/gems/ruby-1.9.3-p125@website/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:474:in `method_missing': undefined method `when_reminder_sent' for EnterpriseRegistrationTest:Class (NoMethodError)
from /Users/elephanttrip/Sites/website/test/unit/enterprise_registration_test.rb:578:in `block (5 levels) in <class:EnterpriseRegistrationTest>'
from /Users/elephanttrip/.rvm/gems/ruby-1.9.3-p125@website/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:322:in `instance_exec'
from /Users/elephanttrip/.rvm/gems/ruby-1.9.3-p125@website/gems/shoulda-context-1.1.6/lib/shoulda/context/context.rb:322:in `merge_block'
答案 0 :(得分:0)
所以我只是重写了这个并在测试开始时把它扔了。显然,此模块注入没有正确编码以进行升级。
获奖答案
class MyMassiveObjectTest < ActiveSupport::TestCase
def self.when_reminder_sent(n, &blk)
context "when reminder #{n} was sent" do
setup { subject.free_trial_notice_sent = n }
blk.bind(self).call
end
end
def self.when_payment_was_received(&blk)
context 'when payment has been received' do
setup { subject.last_payment_received_at = 1.day.ago }
blk.bind(self).call
end
end