【发布时间】:2011-02-28 10:22:25
【问题描述】:
Rails 似乎不会加载任何用于单元或功能测试的装置。我有一个简单的“products.yml”,可以解析并显示正确:
ruby:
title: Programming Ruby 1.9
description:
Ruby is the fastest growing and most exciting dynamic
language out there. If you need to get working programs
delivered fast, you should add Ruby to your toolbox.
price: 49.50
image_url: ruby.png
我的控制器功能测试开始于:
require 'test_helper'
class ProductsControllerTest < ActionController::TestCase
fixtures :products
setup do
@product = products(:one)
@update = {
:title => 'Lorem Ipsum' ,
:description => 'Wibbles are fun!' ,
:image_url => 'lorem.jpg' ,
:price => 19.95
}
end
根据这本书,Rails 应该“神奇地”加载固定装置(因为我的 test_helper.rb 里面有 fixtures :all。我还添加了显式固定装置加载(见上文)。是的 Rails 抱怨:
user @ host ~/Dropbox/Rails/depot > rake test:functionals
(in /Somewhere/Users/user/Dropbox/Rails/depot)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/products_controller_test.rb"
Loaded suite /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
EEEEEEE
Finished in 0.062506 seconds.
1) Error:
test_should_create_product(ProductsControllerTest):
NoMethodError: undefined method `products' for ProductsControllerTest:Class
/test/functional/products_controller_test.rb:7
2) Error:
test_should_destroy_product(ProductsControllerTest):
NoMethodError: undefined method `products' for ProductsControllerTest:Class
/test/functional/products_controller_test.rb:7
...
我确实遇到了另一个 Rails 测试夹具问题 Rails unit testing doesn't load fixtures,但这会导致插件问题(与加载夹具的顺序有关)。
顺便说一句,我正在使用 Rail 2.3.5 和 Ruby 1.8.7 的 Mac OS X 10.6 上进行开发,没有额外的插件(除了基本安装)。
关于如何调试的任何指示,为什么 Rails 的魔力在这里似乎失败了?是版本问题吗?我可以将代码跟踪到库中并找到答案吗?有这么多“mixin”模块,我找不到 fixtures 方法真正存在的地方。
【问题讨论】:
-
APIdock 在您需要查找方法文档的情况下非常有用。 apidock.com/rails/ActiveRecord/TestFixtures/ClassMethods/…
-
感谢 x1a4! apidocks 是一个非常方便的工具,我将添加到我的工具箱中。仍然不确定根本原因是什么......
标签: ruby-on-rails unit-testing fixtures