我怎么能期望用特定的块调用特定的方法?
How can I expect that a certain method is called with a specific block?
我希望能够使用 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
我希望能够使用 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