在嵌套的Rspec上下文中继承示例

Dmy*_*iak 4 ruby testing rspec ruby-on-rails

如何重用这些示例,以便只覆盖嵌套上下文的详细信息?

像这样的东西(我使用thee而不是它表明它是在嵌套的上下文中执行的.它不在RSpec中,正是我想要的):

describe "Abilities" do
  subject { Abilities.new user }

  context "allowed" do
    let(:user) { Factory(:power_user) }
    thee { should be_able_to :create, object }
    thee { should be_able_to :read, object }
    thee { should be_able_to :update, object }

    context "comment" do
      let(:object) { Factory(:comment) }
    end

    context "post" do
      let(:object) { Factory(:post) }
    end

    context "blog" do
      let(:object) { Factory(:blog) }
    end

  end
end
Run Code Online (Sandbox Code Playgroud)

这个例子最终会有3个上下文(评论,帖子,博客)的3个例子(创建,阅读,更新),总共有9个例子.

如何实现它(没有编写共享示例)?

Ell*_*ler 6

没有办法继承示例,但您可以创建一个类方法:

describe "Abilities" do
  subject { Abilities.new user }

  def self.should_allow_stuff
    it { should be_able_to :create, object }
    it { should be_able_to :read, object }
    it { should be_able_to :update, object }
  end

  context "allowed" do
    let(:user) { Factory(:power_user) }

    context "comment" do
      let(:object) { Factory(:comment) }
      should_allow_stuff
    end

    context "post" do
      let(:object) { Factory(:post) }
      should_allow_stuff
    end

    context "blog" do
      let(:object) { Factory(:blog) }
      should_allow_stuff
    end

  end
end
Run Code Online (Sandbox Code Playgroud)

如果您愿意,可以根据需要进行重构.