Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 继承嵌套 中的示例;Rspec上下文_Ruby On Rails_Ruby_Testing_Rspec - Fatal编程技术网

Ruby on rails 继承嵌套 中的示例;Rspec上下文

Ruby on rails 继承嵌套 中的示例;Rspec上下文,ruby-on-rails,ruby,testing,rspec,Ruby On Rails,Ruby,Testing,Rspec,如何重用这些示例,以便只覆盖嵌套上下文中的细节 类似这样的东西(我使用e而不是它,它表明它是在嵌套上下文中执行的。它不在RSpec中,这正是我想要的): 本例将以3个上下文(评论、帖子、博客)的3个示例(创建、阅读、更新)结束,总共产生9个示例 如何实现它(不编写共享示例)?为什么不编写共享示例?这正是它们的用途。没有继承示例的方法,但您可以创建一个类方法: describe "Abilities" do subject { Abilities.new user } def self.

如何重用这些示例,以便只覆盖嵌套上下文中的细节

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

本例将以3个上下文(评论、帖子、博客)的3个示例(创建、阅读、更新)结束,总共产生9个示例


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

为什么不编写共享示例?这正是它们的用途。

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

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

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

在这里编写共享示例感觉有些过火。在很多情况下,共享示例只应用一次。因此,这些共享示例根本不被共享。如果共享示例没有在全球范围内保留,那么我会说使用它们,但因为它们没有(至少上次我检查时是这样),我同意它们有点过火。注意:我不再推荐这种共享示例的方式。RSpec中的共享上下文现在位于上下文内部,并且不会渗透到全局范围,因此您应该使用它们。或者,因为共享示例可能会让人困惑,无法独立运行,也难以调试,所以我会考虑构建自己的匹配器。
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