我希望能够用rspec测试一个特定的gem调用一个特定的块:
代码如下所示
SomeGem.configure do |config|
config.username = "hello"
config.password = "world"
end
和我写的规范看起来像这样:
it 'sets valid gem configuration' do
credentials = lambda do |config|
config.username = "hello"
config.password = "world"
end
expect(SomeGem).to receive(:configure).with(credentials)
end
我得到的错误:
Failure/Error: expect(SomeGem).to receive(:configure).with(credentials)
Wrong number of arguments. Expected 0, got 1.
关于我应该如何测试这个有什么想法吗?
我宁愿尝试断言外部可见的效果。假设您有一个可以用来检索配置值的SomeGem.configuration
方法,那么您可以写入
describe 'configuration block' do
subject do
lambda do
SomeGem.configure do |config|
config.username = "hello"
config.password = "world"
end
end
end
it { is_expected.to change(SomeGem.configuration, :username).to("hello") }
it { is_expected.to change(SomeGem.configuration, :password).to("world") }
end