使用 RSpec 2 关闭一种规格的交易装置

发布于 2024-09-26 10:35:41 字数 410 浏览 2 评论 0原文

如何使用 RSpec 2 关闭仅一种规格(或 Steak 场景)的事务装置? 我尝试了一些在网上找到的东西但没有成功。

这会导致未定义的方法异常。

describe "MyClass without transactional fixtures" do
  self.use_transactional_fixtures = false
  ...
end

这根本没有任何作用(交易装置仍然有效):

describe "MyClass without transactional fixtures" do
  RSpec.configure do |config|
    config.use_transactional_fixtures = false
  end
  ...
end

我还能尝试什么?

How do I turn off transactional fixtures for only one spec (or Steak scenario) with RSpec 2?
I tried some things found on the web without any success.

This leads to an undefined method exception.

describe "MyClass without transactional fixtures" do
  self.use_transactional_fixtures = false
  ...
end

This simply does nothing (transactional fixture is still on):

describe "MyClass without transactional fixtures" do
  RSpec.configure do |config|
    config.use_transactional_fixtures = false
  end
  ...
end

What else could I try?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

终止放荡 2024-10-03 10:35:42

使用 Rspec 2.3.8 时,使用 use_transactional_tests 而不是 use_transactional_fixtures

def without_transactional_fixtures(&block)
  self.use_transactional_tests = false

  before(:all) do
    DatabaseCleaner.strategy = :truncation
  end

  yield

  after(:all) do
    DatabaseCleaner.strategy = :transaction
  end

  self.use_transactional_tests = true
end

Use use_transactional_tests instead of use_transactional_fixtures When Rspec 2.3.8 is being used

def without_transactional_fixtures(&block)
  self.use_transactional_tests = false

  before(:all) do
    DatabaseCleaner.strategy = :truncation
  end

  yield

  after(:all) do
    DatabaseCleaner.strategy = :transaction
  end

  self.use_transactional_tests = true
end
南街九尾狐 2024-10-03 10:35:42

RSpec 6.0 的 2023 年答案:

uses_transaction "doesn't run in transaction"

it "doesn't run in transaction" do
  expect(ActiveRecord::Base.connection.transaction_open?).to eq(false)
end

it "runs in transaction" do
  expect(ActiveRecord::Base.connection.transaction_open?).to eq(true)
end

https://github.com/rspec/rspec-rails/blob/v6.0.1/spec/rspec/rails/fixture_support_spec.rb#L21

The 2023 answer for RSpec 6.0:

uses_transaction "doesn't run in transaction"

it "doesn't run in transaction" do
  expect(ActiveRecord::Base.connection.transaction_open?).to eq(false)
end

it "runs in transaction" do
  expect(ActiveRecord::Base.connection.transaction_open?).to eq(true)
end

https://github.com/rspec/rspec-rails/blob/v6.0.1/spec/rspec/rails/fixture_support_spec.rb#L21

仅此而已 2024-10-03 10:35:42

我只需要关闭套件中的一项测试的事务装置。上面写的对我来说没有任何作用(Rails 6.1.7.6)。

我必须像这样编写测试设置

describe '.method', use_transactional_fixtures: false, truncate: true do
end

,并且在 Rspec.config 块内的 Rails 帮助程序中,我添加了以下代码:

  config.before do |example|
    config.use_transactional_fixtures = (example.metadata[:use_transactional_fixtures] || true)

    DatabaseCleaner.strategy = if example.metadata[:truncate]
                                 :truncation
                               else
                                 :transaction
                               end
    DatabaseCleaner.start
  end

  config.after do
    config.use_transactional_fixtures = true
  end

  config.append_after do
    DatabaseCleaner.clean
  end

这允许我为套件中的所有其他测试保留事务装置

I needed to switch off transactional fixtures for only one test in the suite. Nothing written above worked for me (Rails 6.1.7.6).

I had to write the test setup like that

describe '.method', use_transactional_fixtures: false, truncate: true do
end

And in the rails helper inside Rspec.config block, I added following code:

  config.before do |example|
    config.use_transactional_fixtures = (example.metadata[:use_transactional_fixtures] || true)

    DatabaseCleaner.strategy = if example.metadata[:truncate]
                                 :truncation
                               else
                                 :transaction
                               end
    DatabaseCleaner.start
  end

  config.after do
    config.use_transactional_fixtures = true
  end

  config.append_after do
    DatabaseCleaner.clean
  end

This allowed me keeping transactional fixtures for all other tests in the suite

莫言歌 2024-10-03 10:35:42

感谢Maxence的回答,我发现了非常实用的技巧。

您可以在任何描述/上下文块中编写这个神奇的短语def self.uses_transaction?(_method) = true

例如:

describe "transactional test" do # or context
  def self.uses_transaction?(_method) = true

  # any testcase in this block is out of transaction
end

Thanks for Maxence's answer, I found very practical hack.

You can write this magical phrase def self.uses_transaction?(_method) = true in any describe/context block.

For example:

describe "transactional test" do # or context
  def self.uses_transaction?(_method) = true

  # any testcase in this block is out of transaction
end
烟凡古楼 2024-10-03 10:35:41

我通常添加一个像这样的助手:

def without_transactional_fixtures(&block)
  self.use_transactional_fixtures = false

  before(:all) do
    DatabaseCleaner.strategy = :truncation
  end

  yield

  after(:all) do
    DatabaseCleaner.strategy = :transaction
  end
end

它可以让我关闭规范中特定块的事务装置:

describe "doing my thing" do
  without_transactional_fixtures do
    it "does something without transaction fixtures" do
      ...
    end
  end
end

I usually add a helper like this:

def without_transactional_fixtures(&block)
  self.use_transactional_fixtures = false

  before(:all) do
    DatabaseCleaner.strategy = :truncation
  end

  yield

  after(:all) do
    DatabaseCleaner.strategy = :transaction
  end
end

Which lets me turn off transactional fixtures for a specific block in the specs:

describe "doing my thing" do
  without_transactional_fixtures do
    it "does something without transaction fixtures" do
      ...
    end
  end
end
眼泪淡了忧伤 2024-10-03 10:35:41

我用database_cleaner这样做是为了测试使用事务的代码(这将与transactional_fixtures或任何其他进行事务测试的策略冲突,例如DatabaseCleaner.strategy =:truncation或:transaction):

# spec_helper.rb
config.use_transactional_fixtures = false
config.around(:each, :testing_transactions => true) do |ex|
    DatabaseCleaner.strategy = nil
    ex.run
    DatabaseCleaner.strategy = :truncation
end

在我的测试用例中:

it "should not save if one of objects are invalid", :testing_transactions => true

I've did it this way, with database_cleaner, in order to test code that uses transactions (which will conflict with transactional_fixtures or any other strategy to make transactional tests e.g. DatabaseCleaner.strategy = :truncation or :transaction):

# spec_helper.rb
config.use_transactional_fixtures = false
config.around(:each, :testing_transactions => true) do |ex|
    DatabaseCleaner.strategy = nil
    ex.run
    DatabaseCleaner.strategy = :truncation
end

and in my test cases:

it "should not save if one of objects are invalid", :testing_transactions => true
眼中杀气 2024-10-03 10:35:41

我混合了两个答案,它在 RSpec 3 上对我有用:

config.around(:each, use_transactional_fixtures: false) do |example|
  self.use_transactional_fixtures = false
  example.run
  self.use_transactional_fixtures = true

  DatabaseCleaner.clean_with(:deletion)
end

然后你可以在描述、上下文或它块中使用它

describe 'my test', use_transactional_fixtures: false do
   ...
end

I mixed both answers and it worked for me on RSpec 3:

config.around(:each, use_transactional_fixtures: false) do |example|
  self.use_transactional_fixtures = false
  example.run
  self.use_transactional_fixtures = true

  DatabaseCleaner.clean_with(:deletion)
end

You can then use it in the describe, context or it block

describe 'my test', use_transactional_fixtures: false do
   ...
end
拥抱没勇气 2024-10-03 10:35:41

这曾经是一个错误(请参阅 ticket #197),但我现在好像没问题了。我只是不知道它是否适用于每个测试基地(可能不会)。如果您想执行此操作,可以通过将 config.use_transactional_fixtures = false 放在 spec_helper.rb 上来全局禁用事务装置,并使用 DatabaseCleaner 进行设置。

在浏览器上使用 javascript 测试页面时,我遇到了类似的问题(这种情况不适用于事务固定装置)。以下是我设法解决这个问题的方法: http://github.com/lailsonbm/contact_manager_app

This used to be a bug (see ticket #197), but I seems to be okay now. I just don't know if it will work on a per test base (probably not). If you want to do this, you can disable transactional fixtures globally by putting config.use_transactional_fixtures = false on the spec_helper.rb and use DatabaseCleaner to set that.

I've had a similar problem when testing pages with javascript on the browser (a scenario that does not work with transactional fixtures). Here's how I managed to work around it: http://github.com/lailsonbm/contact_manager_app

一百个冬季 2024-10-03 10:35:41

不确定这是否适用于 RSpec2,但适用于 3。

config.use_transactional_fixtures = true
config.around(:each, use_transactional_fixtures: false) do |example|
  self.use_transactional_tests = false
  example.run
  self.use_transactional_tests = true
end

请注意 use_transactional_fixtures(rspec-rails 选项)和 use_transactional_tests(activerecord Fixtures 选项)的差异。

Not sure if that applies to RSpec2, but works fine with 3.

config.use_transactional_fixtures = true
config.around(:each, use_transactional_fixtures: false) do |example|
  self.use_transactional_tests = false
  example.run
  self.use_transactional_tests = true
end

Mind the use_transactional_fixtures (rspec-rails option) and use_transactional_tests (activerecord fixtures option) difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文