如何将固定装置隔离到特定的 Rails 测试
How to isolate fixtures to specific Rails tests
如何将固定装置的使用隔离到特定测试?
在我的设置中,我的一些测试依赖于夹具数据,而另一些则不依赖,因此使用 fixtures :all
在 test_helper.rb 中加载我的所有夹具的默认设置会破坏我的测试。
需要空行为主义者的示例集成测试table:
require 'test_helper'
class WelcomeFlowTest < ActionDispatch::IntegrationTest
test "when no user is found start welcome flow" do
get "/"
follow_redirect!
assert_response :success
post "/setup", {
behaviorist: { name: "Andy", email: "andy.bettisworth@accreu.com" },
habit: { name: "Interval running", on_monday: false, on_tuesday: true, \
on_wednesday: false, on_thursday: true, on_friday: false, \
on_saturday: true, on_sunday: false }
}
assert_response :success
assert_equal 1, Behaviorist.count
assert_equal 1, Habit.count
end
end
我的单元测试需要行为主义装置:
require 'test_helper'
class BehavioristTest < ActiveSupport::TestCase
test "validates uniqueness of :name" do
andy = Behaviorist.new(name: "Andy", remote_ip: "127.0.0.1")
assert_not run.valid?
assert_match /has already been taken/, andy.errors[:name].join
end
end
通过深入了解 Rails 如何实现固定装置,我发现固定装置一旦加载,就会通过事务与每个 TestCase 中的更改隔离开来。我的工作解决方案是删除在 test_helper.rb 中加载 fixtures :all
。然后对于每个需要固定装置的测试,我覆盖使用事务固定装置的默认值,加载特定固定装置,然后在拆卸时删除它们。
单个测试用例的隔离夹具示例:
require 'test_helper'
class BehavioristTest < ActiveSupport::TestCase
self.use_transactional_fixtures = false
fixtures :behaviorists
teardown :delete_behaviorists
test "validates uniqueness of :name" do
andy = Behaviorist.new(name: "Andy", remote_ip: "127.0.0.1")
assert_not run.valid?
assert_match /has already been taken/, run.errors[:name].join
end
private
def delete_behaviorists
Behaviorist.delete_all
end
end
如何将固定装置的使用隔离到特定测试?
在我的设置中,我的一些测试依赖于夹具数据,而另一些则不依赖,因此使用 fixtures :all
在 test_helper.rb 中加载我的所有夹具的默认设置会破坏我的测试。
需要空行为主义者的示例集成测试table:
require 'test_helper'
class WelcomeFlowTest < ActionDispatch::IntegrationTest
test "when no user is found start welcome flow" do
get "/"
follow_redirect!
assert_response :success
post "/setup", {
behaviorist: { name: "Andy", email: "andy.bettisworth@accreu.com" },
habit: { name: "Interval running", on_monday: false, on_tuesday: true, \
on_wednesday: false, on_thursday: true, on_friday: false, \
on_saturday: true, on_sunday: false }
}
assert_response :success
assert_equal 1, Behaviorist.count
assert_equal 1, Habit.count
end
end
我的单元测试需要行为主义装置:
require 'test_helper'
class BehavioristTest < ActiveSupport::TestCase
test "validates uniqueness of :name" do
andy = Behaviorist.new(name: "Andy", remote_ip: "127.0.0.1")
assert_not run.valid?
assert_match /has already been taken/, andy.errors[:name].join
end
end
通过深入了解 Rails 如何实现固定装置,我发现固定装置一旦加载,就会通过事务与每个 TestCase 中的更改隔离开来。我的工作解决方案是删除在 test_helper.rb 中加载 fixtures :all
。然后对于每个需要固定装置的测试,我覆盖使用事务固定装置的默认值,加载特定固定装置,然后在拆卸时删除它们。
单个测试用例的隔离夹具示例:
require 'test_helper'
class BehavioristTest < ActiveSupport::TestCase
self.use_transactional_fixtures = false
fixtures :behaviorists
teardown :delete_behaviorists
test "validates uniqueness of :name" do
andy = Behaviorist.new(name: "Andy", remote_ip: "127.0.0.1")
assert_not run.valid?
assert_match /has already been taken/, run.errors[:name].join
end
private
def delete_behaviorists
Behaviorist.delete_all
end
end