具有shared_context的规范

时间:2015-02-05 19:20:57

标签: ruby-on-rails ruby rspec

在RSpec 2.99上做一些规范,我在使用shared_context时遇到了问题。以下代码有效:

shared_context 'getting a private taxon' do
  subject { create :category, public: false }
end

shared_context 'getting a public taxon with a private parent' do
  let(:parent) { create :category, public: false }
  subject { create :category, public: true, parent: parent }
end

describe 'GET taxons/permalink', type: :feature do
  context 'only_public: true' do
    before(:each) { get taxon_permalink_api_taxons_path(permalink: subject.permalink, only_public: true) }
    include_context 'getting a private taxon' do
      it { expect(response.status).to be 404 }
    end
    include_context 'getting a public taxon with a private parent' do
      it { expect(response.status).to be 404 }
    end
    context 'getting a public taxon' do
      subject { create :category, public: true }
      it { expect(response.status).to be 200 }
    end
  end

  context 'only_public: false' do
    before(:each) { get taxon_permalink_api_taxons_path(permalink: subject.permalink, only_public: false) }
    include_context 'getting a private taxon' do
      it { expect(response.status).to be 200 }
    end
    include_context 'getting a public taxon with a private parent' do
      it { expect(response.status).to be 200 }
    end
    context 'getting a public taxon' do
      subject { create :category, public: true }
      it { expect(response.status).to be 200 }
    end
  end
end

然而,当我想重构上下文并获得一个公共分类单元时,到shared_context,就像这样:

shared_context 'getting a private taxon' do
  subject { create :category, public: false }
end

shared_context 'getting a public taxon with a private parent' do
  let(:parent) { create :category, public: false }
  subject { create :category, public: true, parent: parent }
end

shared_context 'getting a public taxon' do
  subject { create :category, public: true }
end

describe 'GET taxons/permalink', type: :feature do
  context 'only_public: true' do
    before(:each) { get taxon_permalink_api_taxons_path(permalink: subject.permalink, only_public: true) }
    include_context 'getting a private taxon' do
      it { expect(response.status).to be 404 }
    end
    include_context 'getting a public taxon with a private parent' do
      it { expect(response.status).to be 404 }
    end
    include_context  'getting a public taxon' do
      it { expect(response.status).to be 200 }
    end
  end

  context 'only_public: false' do
    before(:each) { get taxon_permalink_api_taxons_path(permalink: subject.permalink, only_public: false) }
    include_context 'getting a private taxon' do
      it { expect(response.status).to be 200 }
    end
    include_context 'getting a public taxon with a private parent' do
      it { expect(response.status).to be 200 }
    end
    include_context 'getting a public taxon' do
      it { expect(response.status).to be 200 }
    end
  end
end

我的规格停止工作,似乎总是错误地设置主题。

PD:我不知道这是否是在不重复代码的情况下在两个规格之间共享夹具的正确方法。任何建议都被接受。

0 个答案:

没有答案