在Rails 6中,测试枚举_scopes的正确方法如下:
your_model_test.rb
,其中your_model
是你要测试的模型名称。require 'test_helper'
require 'your_model'
ActiveSupport::TestCase
:class YourModelTest < ActiveSupport::TestCase
# 测试代码将放在这里
end
test
关键字定义一个测试方法,并给出一个有意义的名称:class YourModelTest < ActiveSupport::TestCase
test "should return correct records for enum scopes" do
# 测试代码将放在这里
end
end
FactoryBot
或手动创建测试数据。class YourModelTest < ActiveSupport::TestCase
test "should return correct records for enum scopes" do
# 创建测试数据
your_model1 = YourModel.create(status: :active)
your_model2 = YourModel.create(status: :inactive)
# 测试代码将放在这里
end
end
assert_includes
来验证返回的记录是否包含预期的值。class YourModelTest < ActiveSupport::TestCase
test "should return correct records for enum scopes" do
# 创建测试数据
your_model1 = YourModel.create(status: :active)
your_model2 = YourModel.create(status: :inactive)
# 验证枚举_scopes
assert_includes YourModel.active, your_model1
assert_not_includes YourModel.active, your_model2
assert_includes YourModel.inactive, your_model2
assert_not_includes YourModel.inactive, your_model1
end
end
rails test
这样,你就可以正确测试Rails 6中的枚举_scopes了。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云