我可以在这里使用“is_expected”吗?

时间:2016-12-15 04:50:49

标签: ruby rspec

我有以下规格:

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

3 个答案:

答案 0 :(得分:1)

您可以使用after

执行此操作
after { subject }
it { expect(Foo).not_to receive(:bar) }

答案 1 :(得分: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),因此如果你有多个,它可以节省重复例子。

答案 2 :(得分:0)

你可以这样做

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

我已经完成了几次,我的一些同事发现这种风格模糊不清

相关问题