如何设置一个Minitest规范,使其可以访问与IntegrationTest
相同的Rails数据和方法?
我一直使用“旧的” Minitest样式,并决定使用Spec样式添加新的测试文件。但是,这个测试文件
require 'minitest/spec'
require 'minitest/autorun'
describe "Post Controller" do
describe "#index" do
it "renders index view" do
get posts_url
end
end # #index
end
出现以下错误消息:
Error:
Post Controller::#index#test_0001_renders index view:
NameError: undefined local variable or method `posts_url' for #<#<Class:0x00007fe064d71210>:0x00007fe064da8260>
test/controllers/posts_controller_spec_test.rb:7:in `block (3 levels) in <main>'
我怀疑我需要做些什么,以便将规范放在测试控制器的上下文中(从而使路由和其他内容可用);但是,我不知道如何。
答案 0 :(得分:0)
我遇到了同样的问题,并根据此链接上的答案解决了How to tie Minitest spec to IntegrationTest / Rails data
将“describe “Post Controller” do”更改为“class PostControllerSpec < ActionDispatch::IntegrationTest”
require 'minitest/spec'
require 'minitest/autorun'
class PostControllerSpec < ActionDispatch::IntegrationTest
describe "#index" do
it "renders index view" do
get posts_url
end
end # #index
end