加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在rspec中更改挂钩之前的顺序

发布时间:2020-12-17 03:05:31 所属栏目:百科 来源:网络整理
导读:我有一个这样的控制器规格: 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(no
我有一个这样的控制器规格:

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

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

它试图这样做:

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

我在“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

但我觉得它不太干净.

我发现,在这个页面上,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

在这个例子中并非如此.

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

有解决方案吗?

解决方法

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

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

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

也就是说,有一些选择:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读