我想使用should_receive
作为RSpec规范的主题。换句话说,我不想测试should ==
,而是测试另一个类接收方法调用。RSpec:should_receive作为指定的东西
我想什么写的是一样的东西:
describe '#methodOne' do
subject { ClassTwo }
it { should_receive(:methodTwo) }
ClassOne.methodOne
end
这可能吗?
我想使用should_receive
作为RSpec规范的主题。换句话说,我不想测试should ==
,而是测试另一个类接收方法调用。RSpec:should_receive作为指定的东西
我想什么写的是一样的东西:
describe '#methodOne' do
subject { ClassTwo }
it { should_receive(:methodTwo) }
ClassOne.methodOne
end
这可能吗?
因此,这里是我是如何做的:
describe "#methodOne" do
it "calls the appropriate method" do
ClassTwo.should_receive(:methodTwo)
ClassOne.methodOne
end
end
这精美的作品
我会用在它的实例变量形式的主题:
@subject.should_receive(:methodTwo)
有很多方法,你可以扭曲的RSpec做这样的事情,但它不使用RSpec的它是如何打算。 should_receive
设置了一个模拟的期望,即一个方法将被调用。对于单线程it
语法没有意义。
我建议您从基本的describe
/it
结构开始,只有在重复时重构更高级的结构(主题,单行it
)。
我怎么能重写我的代码,以便它测试'should_receive',以RSpec的目的? – amindfv 2012-07-10 14:32:48
这并不为我工作。它跳过了规范,就像没有任何测试。 – amindfv 2012-07-10 14:47:01
是的,你应该嘲笑你的主题类,并使用'should_receive',因为这是RSpec :: Mocks库的一部分。如果你需要的话,这里是一个不错的教程。 – xiy 2012-07-10 14:59:37