在功能说明中提交表单时,我想检查模型中的许多更改。例如,我要确保将用户名从X更改为Y,并且将加密密码更改为任何值。
我知道已经有一些问题了,但是我没有找到合适的答案。最准确的答案似乎ChangeMultiple
是迈克尔·约翰斯顿(Michael Johnston)的匹配者:RSpec是否有可能期望在两个表中进行更改?。它的缺点是只能检查从已知值到已知值的显式更改。
我创建了一些伪代码,说明我认为更好的匹配器看起来像:
expect {
click_button 'Save'
}.to change_multiple { @user.reload }.with_expectations(
name: {from: 'donald', to: 'gustav'},
updated_at: {by: 4},
great_field: {by_at_leaset: 23},
encrypted_password: true, # Must change
created_at: false, # Must not change
some_other_field: nil # Doesn't matter, but want to denote here that this field exists
)
我还创建了ChangeMultiple
匹配器的基本骨架,如下所示:
module RSpec
module Matchers
def change_multiple(receiver=nil, message=nil, &block)
BuiltIn::ChangeMultiple.new(receiver, message, &block)
end
module BuiltIn
class ChangeMultiple < Change
def with_expectations(expectations)
# What to do here? How do I add the expectations passed as argument?
end
end
end
end
end
但是现在我已经收到此错误:
Failure/Error: expect {
You must pass an argument rather than a block to use the provided matcher (nil), or the matcher must implement `supports_block_expectations?`.
# ./spec/features/user/registration/edit_spec.rb:20:in `block (2 levels) in <top (required)>'
# /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
# /Users/josh/.rvm/gems/ruby-2.1.0@base/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
高度赞赏在创建此自定义匹配器方面的任何帮助。
.and change { @something }.by(0)