如何将夹具的使用与特定测试区分开?
在我的设置中,我的一些测试依赖灯具数据,有些则不依赖灯具,因此将我的所有灯具加载到test_helper.rb中的默认设置fixtures :all
会破坏我的测试。
需要空的行为主义者表的示例集成测试:
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: "[email protected]" },
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中的更改隔离开来。我的工作解决方案是删除fixtures :all
内部的test_helper.rb。然后,对于需要夹具的每个测试,我都会覆盖使用事务性夹具,加载特定夹具的默认设置,然后在拆卸时将其删除。
单个TestCase的隔离夹具示例:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句