我正在使用Rails 6.0.3.2 and Ruby 2.7.1.p83构建一个新应用程序的数据支架。在创建了这个应用程序之后,我通过命令行生成了一系列的~60脚手架--每个脚手架都有一个“name”(string)字段。
Rails db:migrate没有问题。当我调用rails test时,我得到527个断言中的2个失败。两者都是“行为”控制器中的NoMethodErrors (缺失的count方法)。
这只发生在一个60英寸的脚手架,这意味着(在这一点上)相同的建造。
这绝对不是预料中的事。有人以前见过这个吗?
来自rails test:的错误消息
BehaviorsControllerTest#test_should_create_behavior:
NoMethodError: undefined method 'count' for ActionDispatch::IntegrationTest::Behavior:Module
test/controllers/behaviors_controller_test.rb:19:in 'block in <class:BehaviorsControllerTest>'
test/controllers/behaviors_controller_test.rb:19:in 'block in <class:BehaviorsControllerTest>'
rails test test/controllers/behaviors_controller_test.rb:18
E
Error:
BehaviorsControllerTest#test_should_destroy_behavior:
NoMethodError: undefined method 'count' for ActionDispatch::IntegrationTest::Behavior:Module
test/controllers/behaviors_controller_test.rb:42:in 'block in <class:BehaviorsControllerTest>'
test/controllers/behaviors_controller_test.rb:42:in 'block in <class:BehaviorsControllerTest>'控制器测试条款:
test "should create behavior" do
assert_difference('Behavior.count') do
post behaviors_url, params: { behavior: { name: @behavior.name } }
end
assert_redirected_to behavior_url(Behavior.last)
endtest "should destroy behavior" do
assert_difference('Behavior.count', -1) do
delete behavior_url(@behavior)
end
assert_redirected_to behaviors_url
end发布于 2020-07-30 16:46:23
您已经选择了一个与Rails内置对象冲突的名称:Behavior。
有一个叫做ActionDispatch::IntegrationTest::Behavior的模块。
测试(想必)是在ActionDispatch::IntegrationTest模块中定义的方法中进行评估的,这样Ruby常量查找在遍历到顶层常量之前就会找到ActionDispatch::IntegrationTest::Behavior。
您需要使用范围解析操作符(::)来引用顶级常量:
assert_difference('::Behavior.count') do
post behaviors_url, params: { behavior: { name: @behavior.name } }
endhttps://stackoverflow.com/questions/63160186
复制相似问题