在rspec中更改挂钩之前的顺序

Dou*_*gui 4 rspec ruby-on-rails ruby-on-rails-3

我有一个这样的控制器规格:

describe "#create" do
  before { post 'create', params }
  context "when the artist is valid" do
    before { allow(artist).to receive(:save).and_return(true) }
    it { expect(page).to redirect_to(root_path) }
    it { expect(notifier).to have_received(:notify) }
  end
end
Run Code Online (Sandbox Code Playgroud)

这是一个简单的规范,但它不起作用,因为在块之前的上下文之前执行了块之前的描述.因此,artist.save调用create动作时,结果不会被存根.

它试图这样做:

describe "first describe" do
  before { puts 2 }
  describe "second describe" do
    before { puts 1 }
    it "simple spec" do
      expect(1).to eq 1
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我在"1"之前看到"2".我不确定,但我认为它适用于以前的版本.

我知道,我可以这样做:

describe "#create" do
  context "when the artist is valid" do
    before { allow(artist).to receive(:save).and_return(true) }
    it "redirect to the root path" do
      post 'create', params
      expect(page).to redirect_to(root_path)
    end

    it "do notifications" do
      post :create, params
      expect(notifier).to have_received(:notify)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但我觉得它不太干净.

我发现,在这个页面上,http: //rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks#before-instance_method的顺序应该是这样:

before(:suite) # declared in RSpec.configure
before(:all)   # declared in RSpec.configure
before(:all)   # declared in a parent group
before(:all)   # declared in the current group
before(:each)  # declared in RSpec.configure
before(:each)  # declared in a parent group
before(:each)  # declared in the current group
Run Code Online (Sandbox Code Playgroud)

在这个例子中并非如此.

我不确定,但我认为它适用于旧版本的rspec.

有解决方案吗?

Joh*_*gan 6

我强烈建议您不要在rspec中更改挂钩的顺序.这将使您的应用程序不标准,Rails基于标准并使事情按预期工作.

你所描述的一切都"按照设计".块之前的外部总是在内部块之前调用.

您觉得"不太干净"的示例是执行控制器规范的标准方法.我实际上鼓励你这样做,以便它更易于维护/读取.它根本不是我的不洁净.

也就是说,有一些选择:

  1. 您可以使用方法.我不止一次有过do_post类似的方法
  2. 您可以使用let懒惰初始化的块.如果它先依赖于其他块先运行,我会发现它是unlcean,但它是一个选项.
  3. 你可以定义subject.https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/subject/explicit-subject