根据spec section within the minitest docs:gem允许您使用测试的“unit_test”语法或测试的“spec style”语法。
在我的rails应用程序中:我对模型中的作用域进行了两次测试(注意:我正在使用factory_girl在我的测试中构建对象)。根据我的理解,以下是在rails的上下文中使用minitest编写测试的典型方法:
# test/models/city_test.rb
require 'test_helper'
class CityTest < ActiveSupport::TestCase
test ".with_active_schools should return none" do
city1 = create(:sequenced_city)
city2 = create(:sequenced_city)
create(:sequenced_inactive_school, city: city1)
create(:sequenced_inactive_school, city: city1)
create(:sequenced_inactive_school, city: city2)
create(:sequenced_inactive_school, city: city2)
assert_equal(0, City.with_active_schools.size)
end
test ".with_active_schools should return all" do
city1 = create(:sequenced_city)
city2 = create(:sequenced_city)
create(:sequenced_active_school, city: city1)
create(:sequenced_active_school, city: city1)
create(:sequenced_active_school, city: city2)
create(:sequenced_active_school, city: city2)
assert_equal(2, City.with_active_schools.size)
end
end
然后我运行rails test test/models/city_test.rb
并且所有测试都通过。
现在我想看看是否可以将上述测试转换为spec-style语法。我在同一个文件中注释掉上面的所有内容。然后我用这段代码替换它:
# test/models/city_test.rb
require 'test_helper'
describe City do
describe ".with_active_schools" do
it "returns no cities with active schools" do
city1 = create(:sequenced_city)
city2 = create(:sequenced_city)
create(:sequenced_inactive_school, city: city1)
create(:sequenced_inactive_school, city: city1)
create(:sequenced_inactive_school, city: city2)
create(:sequenced_inactive_school, city: city2)
expect(City.with_active_schools.size).to eq(0)
end
it "returns cities with active schools" do
city1 = create(:sequenced_city)
city2 = create(:sequenced_city)
create(:sequenced_active_school, city: city1)
create(:sequenced_active_school, city: city1)
create(:sequenced_active_school, city: city2)
create(:sequenced_active_school, city: city2)
expect(City.with_active_schools.size).to eq(2)
end
end
end
然后我尝试运行测试:rails test test/models/city_test.rb
,但它出错:
对于main:Object(NoMethodError),未定义的方法`describe'
我不确定为什么当minitest文档说它支持这种语法时会抛出这个错误。我确实查看了rails指南进行测试,但model testing section中的内容很少。
答案 0 :(得分:0)
我得到了规范风格的功能,虽然最初的问题是:“为什么规范风格的语法测试不能在导轨中使用minitest开箱即用”仍然没有答案。希望有人可能知道并提供答案。
我需要做的是加载minitest-spec-rails gem:
#Gemfile
group :development, :test do
gem 'minitest-spec-rails', '~> 5.4'
end
然后我只需要转换为MiniTest :: Spec断言语法。似乎rspec匹配器语法不起作用:
#test/models/city_test.rb
describe City do
describe ".with_active_schools" do
it "has no cities have active schools" do
city1 = create(:sequenced_city)
city2 = create(:sequenced_city)
create(:sequenced_inactive_school, city: city1)
create(:sequenced_inactive_school, city: city1)
create(:sequenced_inactive_school, city: city2)
create(:sequenced_inactive_school, city: city2)
City.with_active_schools.size.must_equal(0)
end
it "has all cities have active schools" do
city1 = create(:sequenced_city)
city2 = create(:sequenced_city)
create(:sequenced_active_school, city: city1)
create(:sequenced_active_school, city: city1)
create(:sequenced_active_school, city: city2)
create(:sequenced_active_school, city: city2)
City.with_active_schools.size.must_equal(2)
end
end
end
现在,当我运行rails test test/models/city_test.rb
时,它会起作用。
答案 1 :(得分:0)
您必须require 'minitest/spec'
才能使describe
功能可用。