2016-12-15 100 views
0

我有以下规格:我可以在这里使用“is_expected”吗?

describe 'blah blah blah' do 
    it 'yadda yadda yadda' do 
    expect(Foo).not_to receive(:bar) 
    subject 
    end 
end 

需要明确的是,测试通过,如果Foo.bar不叫。

我很好奇,it可以变成一行,使用is_expected语法?

describe 'blah blah blah' do 
    it { is_expected.to... } 
end 

回答

1

您可以after

after { subject } 
it { expect(Foo).not_to receive(:bar) } 
+0

谢谢你,但我的意思是:可以把它变成使用is_expected语法的单线程,所以我不必调用主题? –

+0

我相信,在这种情况下是不可能的。你可以这样做,如果你的'#bar'改变了一些东西。如果是这样的话,你可以这样做:'{expect {subject} .to {Foo.bars_count} .by(1)}' – unkmas

0

您可以在around(:each)块调用subject做到这一点。

describe 'blah blah blah' do 
    around(:each) { |example| subject; example.run } 
    it { is_expected not_to receive :bar } 
end 

这是不是一个真正的一行程序,但around(:each)运行的组(describe在这种情况下)在每个例子中,因此可以节省重复,如果你有多个实例。

0

你可以这样做

describe 'blah blah blah' do 
    before { expect(Foo).not_to receive(:bar) } 
    it { is_expected } 
end 

我已经做了几次,我的一些同事们发现这种风格是晦涩

相关问题