用于测试嵌套上下文的设置

时间:2019-07-05 14:50:27

标签: rspec

这里是RSpec新手-我正在尝试为具有许多初始状态的控制器制定请求规范。为了测试可能的排列,我嵌套了上下文,但是它们不能像我认为的那样工作。我尝试在rspec api上四处查看,以了解诸如append_before / prepend_before之类的内容是否可能有所帮助,但它们似乎仅在上下文的边界内起作用-而不是跨嵌套级别。

我写的是这个

  context 'old A' do
    before { create :a }

    context 'old B' do
      before { create :b }

      context 'old C' do
        before {create :c}
        before {post '/endpoint'}

        it('foos') {assert true}
        it('bars') {assert true}
      end
      context 'no C' do
        before {post '/endpoint'}

        it('foos') {assert true}
        it('bars') {assert true}
      end
    end
    context 'no B' do
        before {post '/endpoint'}

        it('foos') {assert true}
        it('bars') {assert true}
    end
  end
  context 'no A' do
    before { post '/endpoint' }

    it('foos') { assert true }
    it('bars') { assert true }
  end

我想写的是这样的:

  final_before {post '/endpoint'}
  it_deep('foos') {assert true}
  it_deep('bars') {assert true}
  # implicit foo/bar assertion
  # implicit post
  context 'old A' do
    before { create :a }
    # implicit foo/bar assertion
    # implicit post after creating a

    context 'old B' do
      before { create :b }
      # implicit foo/bar assertion
      # implicit post after creating a and b

      context 'old C' do
        before {create :c}
        # implicit foo/bar assertion
        # implicit post after creating a and b and c
      end
    end
  end 

我所拥有的似乎不太干,可能不适合rspec的正确样式。有人对我如何整理事情有任何建议吗?

1 个答案:

答案 0 :(得分:0)

我最终选择了使用src的另一种方式,因为它允许您在继承孩子的同时覆盖孩子的父母功能。例如,我现在可以做类似的事情:

let!
相关问题