使用 RSpec 2 关闭一种规格的交易装置
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用 Rspec 2.3.8 时,使用
use_transactional_tests
而不是use_transactional_fixtures
Use
use_transactional_tests
instead ofuse_transactional_fixtures
When Rspec 2.3.8 is being usedRSpec 6.0 的 2023 年答案:
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:
https://github.com/rspec/rspec-rails/blob/v6.0.1/spec/rspec/rails/fixture_support_spec.rb#L21
我只需要关闭套件中的一项测试的事务装置。上面写的对我来说没有任何作用(Rails 6.1.7.6)。
我必须像这样编写测试设置
,并且在 Rspec.config 块内的 Rails 帮助程序中,我添加了以下代码:
这允许我为套件中的所有其他测试保留事务装置
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
And in the rails helper inside
Rspec.config
block, I added following code:This allowed me keeping transactional fixtures for all other tests in the suite
感谢Maxence的回答,我发现了非常实用的技巧。
您可以在任何描述/上下文块中编写这个神奇的短语
def self.uses_transaction?(_method) = true
。例如:
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:
我通常添加一个像这样的助手:
它可以让我关闭规范中特定块的事务装置:
I usually add a helper like this:
Which lets me turn off transactional fixtures for a specific block in the specs:
我用database_cleaner这样做是为了测试使用事务的代码(这将与transactional_fixtures或任何其他进行事务测试的策略冲突,例如DatabaseCleaner.strategy =:truncation或:transaction):
在我的测试用例中:
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):
and in my test cases:
我混合了两个答案,它在 RSpec 3 上对我有用:
然后你可以在描述、上下文或它块中使用它
I mixed both answers and it worked for me on RSpec 3:
You can then use it in the describe, context or it block
这曾经是一个错误(请参阅 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 thespec_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
不确定这是否适用于 RSpec2,但适用于 3。
请注意 use_transactional_fixtures(rspec-rails 选项)和 use_transactional_tests(activerecord Fixtures 选项)的差异。
Not sure if that applies to RSpec2, but works fine with 3.
Mind the
use_transactional_fixtures
(rspec-rails option) anduse_transactional_tests
(activerecord fixtures option) difference.