huihua: 相关文章: X
ROR新手请问连mysql数据库的问题.
安装了Mongrel后多出来的提示信息
测试时 invalid argument
<img src=http://www.javaeye.com/images/icon_more.gif/>
看了Agile Web Development with Rails(第一版),一步一步按照书上的例子过来的.
前面看得很顺利,而且代码都能自己编译通过.
到了模型测试的时候,用到了夹具,products.yml, 运行测试的时候总是报image_url参数非法错误!
实在不知道这个错误问题到底是怎样发生的. 请指教一下,呵呵...我是刚学RoR的. :(
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html version_control_book: id: 1 title: Pragmatic Version Control description: How to use version control image_url: http://www.rubyonrails.org/images/awdr2.gif price: 29.95 date_availaible: 2007-05-28 09:00:00 automation_book: id: 2 title: Pragmatic Project Automation description: How to automate your project image_url: http://www.rubyonrails.org/images/awdr2.gif price: 39.95 date_availaible: 2007-05-28 10:00:00
require File.dirname(__FILE__) + '/../test_helper'
class ProductTest < Test::Unit::TestCase fixtures :products
def setup @product = Product.find(1) end
# Replace this with your real tests. def test_create assert_kind_of Product, @product assert_equal 1, @product.id assert_equal "Pragmatic Version Control", @product.title assert_equal "How to use version control", @product.description assert_equal "http://www.rubyonrails.org/images/awdr2.gif", @product.image_url assert_equal 29.95, @product.price assert_equal "2007-05-28 09:00:00", @product.date_available_before_type_cast end end
错误问题描述:
引用Loaded suite test/unit/product_test
Started
E
Finished in 0.297 seconds.
1) Error:
test_create(ProductTest):
Errno::EINVAL: Invalid argument - http://www.rubyonrails.org/images/awdr2.gif
数据库情况: (我已经用rake clone_structure_to_test命令复制了开发库的表结果了)
desc products; +----------------+---------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+---------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | title | varchar(100) | NO | | | | | description | text | NO | | | | | image_url | varchar(200) | NO | | | | | price | decimal(10,2) | NO | | | | | date_available | datetime | NO | | | | +----------------+---------------+------+-----+---------+----------------+ 6 rows in set (0.00 sec)
请各位帮我看看到底该怎样解决这个问题,我知道是参数错误,我估计是因为image_url的内容中含有:,所以报参数错误,但是假如我删除了,又报找不到该文件,我特意把文件放到该目录中去,还是报找不到. 郁闷坏了! gigix: yml里面加上双引号试试 huihua: 我也加双引号过了,错误依然存在,而且一样. 郁闷中..... robbin: 是不是斜线需要转义? huihua: 我也试过,分别在斜杠前家了""和"试过两次,结果都是一样,错误还是一样的错误.
我把所有的信息贴出来吧,大家帮我看看,感谢!
ruby test/unit/product_test.rb DEPRECATION WARNING: model is deprecated and will be removed from Rails 2.0 See http://www.rubyonrails.org/deprecation for details. (called from D:/rubyproject /depot/config/../app/controllers/application.rb:8) DEPRECATION WARNING: depend_on is deprecated and will be removed from Rails 2.0 See http://www.rubyonrails.org/deprecation for details. (called from model_with out_deprecation at C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_c ontroller/deprecated_dependencies.rb:13) DEPRECATION WARNING: model is deprecated and will be removed from Rails 2.0 See http://www.rubyonrails.org/deprecation for details. (called from D:/rubyproject /depot/config/../app/controllers/application.rb:9) DEPRECATION WARNING: depend_on is deprecated and will be removed from Rails 2.0 See http://www.rubyonrails.org/deprecation for details. (called from model_with out_deprecation at C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_c ontroller/deprecated_dependencies.rb:13) Loaded suite test/unit/product_test Started E Finished in 0.266 seconds.
1 tests, 0 assertions, 0 failures, 1 errors lgn21st: huihua 写道
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html version_control_book: id: 1 title: Pragmatic Version Control description: How to use version control image_url: http://www.rubyonrails.org/images/awdr2.gif price: 29.95 date_availaible: 2007-05-28 09:00:00 automation_book: id: 2 title: Pragmatic Project Automation description: How to automate your project image_url: http://www.rubyonrails.org/images/awdr2.gif price: 39.95 date_availaible: 2007-05-28 10:00:00
看了楼主的这个问题我自己也很奇怪,看不出任何问题,动手按照楼主的代码测试了一下,果然结果也跟楼主一样,非常纳闷,在console里面直接手动生成一个Product模型的实例,填充数据,然后dump成YAML看看结果是什麽:
./script/console
require 'yaml' product = Product.new product.id = 1 product.title = 'Pragmatic Version Control' product.description = 'How to use version control' product.image_url = 'http://www.rubyonrails.org/images/awdr2.gif' product.price = 29.95 product.date_available = Time.now
open("product", "w") { |f| YAML.dump(product, f) }
看到结果:
--- !ruby/object:Product attributes: image_url: http://www.rubyonrails.org/images/awdr2.gif date_available: 2007-05-30 20:07:28.926981 +08:00 price: 29.95 title: Pragmatic Version Control id: 1 description: How to use version control new_record: true
将YAML文件转回product对象:
new_product = open("product") { |f| YAML.load(f) }
终于发现问题所在,关键是Yaml文件虽然设计为机器跟人都能读取,但是 'value'之间的空格只能保留一个,看来这是个限制,不测是还真没有发现,虽然之前一直用YAML,并以为非常好用。楼主把多余的空格删除看看结果,另楼主在data_available字段上的数据有错,吧date_available删除,或安正确写法即可 huihua: 非常谢谢楼上的!!
不过我当时还特意注意到这个空格的问题,因为书上特别提示了不能用TABLE键美化格式,只能用空格,可能我多用了几个空格.
再次谢谢楼上的!! huihua: 不对! 我刚才按照您说的,一一测试过,还是不对! (我按照您的说法,每个只有一个空格)
错误还是一模一样!!
时间也肯定没有问题,因为我在开发库里存的时间也是这个格式,完全可以的.
我也按照您说的格式试过,还是不对!!
非常郁闷!!!!!! 晕..........
lgn21st 写道huihua 写道
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html version_control_book: id: 1 title: Pragmatic Version Control description: How to use version control image_url: http://www.rubyonrails.org/images/awdr2.gif price: 29.95 date_availaible: 2007-05-28 09:00:00 automation_book: id: 2 title: Pragmatic Project Automation description: How to automate your project image_url: http://www.rubyonrails.org/images/awdr2.gif price: 39.95 date_availaible: 2007-05-28 10:00:00
看了楼主的这个问题我自己也很奇怪,看不出任何问题,动手按照楼主的代码测试了一下,果然结果也跟楼主一样,非常纳闷,在console里面直接手动生成一个Product模型的实例,填充数据,然后dump成YAML看看结果是什麽:
./script/console
require 'yaml' product = Product.new product.id = 1 product.title = 'Pragmatic Version Control' product.description = 'How to use version control' product.image_url = 'http://www.rubyonrails.org/images/awdr2.gif' product.price = 29.95 product.date_available = Time.now
open("product", "w") { |f| YAML.dump(product, f) }
看到结果:
--- !ruby/object:Product attributes: image_url: http://www.rubyonrails.org/images/awdr2.gif date_available: 2007-05-30 20:07:28.926981 +08:00 price: 29.95 title: Pragmatic Version Control id: 1 description: How to use version control new_record: true
将YAML文件转回product对象:
new_product = open("product") { |f| YAML.load(f) }
终于发现问题所在,关键是Yaml文件虽然设计为机器跟人都能读取,但是 'value'之间的空格只能保留一个,看来这是个限制,不测是还真没有发现,虽然之前一直用YAML,并以为非常好用。楼主把多余的空格删除看看结果,另楼主在data_available字段上的数据有错,吧date_available删除,或安正确写法即可 huihua: 我刚才把数据库表重新用sql脚本建了一次,运行的结果还是如此! 痛苦呀!实在想不到问题出在哪里? lgn21st: 楼主不要着急,把错误贴出来,另我贴出来的代码是我自己测试过程中生成的,是调试通过的。
attributes: image_url: http://www.rubyonrails.org/images/awdr2.gif date_available: 2007-05-30 21:49:48.569904 +08:00 price: 29.95 title: Pragmatic Version Control id: 1 description: How to use version control
然后您执行一次 rake 看看,我刚刚执行完毕,fixture载入正常,这个问题不是大问题,再仔细一点,在耐心一点。实在不行就给我发邮件,我把我测试的代码发给您。 lgn21st: 算了,直接打包上传吧,楼主自己看。
demo.zip (66.7 KB)
描述:
下载次数: 10 hgq0011: hehe,我刚也测试了,也和LZ一样出现相同的问题。
假如直接从数据库中获取数据就没有问题。估计是夹具有bug? yehs220: 常常碰到这种错误a YAML error occurred parsing test/fixtures/users.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html您看看您的是不是因为这个 hgq0011: lgn21st 写道: 算了,直接打包上传吧,楼主自己看。
这就奇怪了,用您的代码就算加上断言,也没有问题。是不是我们的"depot"中其它地方和它有冲突? lgn21st: 阿?那么楼上兄台将测试代码贴给楼主吧,我手头上还有其它事情,先跑路了。 hgq0011: lgn21st 写道阿?那么楼上兄台将测试代码贴给楼主吧,我手头上还有其它事情,先跑路了。
就是把您的代码,加上LZ的测试代码在上面,然后测试,啥问题也没有。
不过,刚才我把原来products表中的数据全部清除(也只留了这一个表了,没有关联关系了),然后在测试就没有问题了。估计和这个有关吧。:( lgn21st: 刚才看走眼了,楼主写的测试断言代码没有问题,在老板本的rails中似乎可以把fixture当做一个同名实例变量直接访问,到1.2似乎就不可以了,不过我测试从来都是从测试数据看中取数据测试。 huihua: 我写的断言没有问题,我用您的夹具就可以通过!!! 而且我把您的夹具中":"后故意多添加了很多空格,依然通过! 所以,可以肯定的是,不是空格问题,也不是因为":"和"/"这些字符的问题,也不是数据库的问题.
而且您说的时间问题,我也试过,用我以前写的时间,依然通过.
这我就很纳闷了!
lgn21st 写道刚才看走眼了,楼主写的测试断言代码没有问题,在老板本的rails中似乎可以把fixture当做一个同名实例变量直接访问,到1.2似乎就不可以了,不过我测试从来都是从测试数据看中取数据测试。 lgn21st: 很奇怪,楼主用的是什麽平台?什麽编辑器呀?我在Ubuntu7.04下全部都用vim写代码,系统默认字符编码是utf8.的确yaml没有冒号后面空格限制,我之前的帖子是武断结论了。单yaml格式对空格是很敏感的,很多人包括官方文档都提到。且不能用tab,我在vim里面将tab映射成2个空格,写测试时间也不短了,从来都没有碰到yaml加载方面的问题。楼主关注一下自己的平台,或把出错代码上传大家看看吧。 huihua: 我终于找到问题出在哪里了!!!!!!!!!!!!!!!!!!!!!!!!!
是空格的问题!!!
所有数据库字段的不能顶格写, 必须有一个空格!!!!!
每一个名/值对必须由冒号分开,并且用空格缩进(不能用TABLE键缩进)!!!
我该打!! 书上都注明了要用空格缩进!! 我误看成需要空格分开!! 看书不仔细,让我郁闷了好几天!!
最后,非常谢谢楼上的各位朋友,帮我找问题. 呵呵....我也是从lgn21st给我的那个demo中的夹具代码和我自己写的夹具代码一一对应了一下,没有发现什麽不同,但是直接拷贝它的就能用,我就纳闷了!!
最后发现它的除了第一行,每行前面都有一个空格,除了这个,没有别的不同了.
再该自己的,果然测试通过!!! 再看书本,以为书上应当说了呀!!!
再仔细一看书,想扇自己一耳光,NND!!
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html version_control_book : id : 1 title : Pragmatic Version Control description : How to use version control image_url : http://www.rubyonrails.org/images/awdr2.gif price : 29.95 date_available : 2007-05-30 20:07:28.926981 +08:00 automation_book : id : 2 title: Pragmatic Project Automation description: How to automate your project image_url : http://images.dangdang.com/images/9240772_b.jpg price : 39.95 date_available : 2007-05-30 20:07:28.926981 +08:00 huihua: 但是我还是有纳闷的地方,为什麽它就偏偏说image_url这里参数错误呢!!
而且总是说我上面代码的第13行错误,而不是别的地方呢? 非常奇怪!
而且我调试的过程中,我发现rails把这些字段插入到数据库中的sql语句很奇怪. 是这样的格式:
INSERT INTO products (`image_url`, `price`, `title`, `id`, `description`, `date_available`) VALUES ('http://images.dangdang.com/images/9240772_b.jpg', 39.95, 'Pragmatic Project Automation', 2, 'How to automate your project', '2007-05-30 20:07:28')
但是我在数据库中定义字段的顺序不是这样的,而且我在夹具中写的顺序也不是这样的. 所以我猜测,rails是按照yml文件中的内容倒插入数据库中,字段顺序就不知道怎样排序法了. 这样第一个访问的就是我的yml中的13行的image_url,这样才报这一行的错误了! 只能这样解释了. 呵呵.... lgn21st: lgn21st 写道
看到结果:
--- !ruby/object:Product attributes: image_url: http://www.rubyonrails.org/images/awdr2.gif date_available: 2007-05-30 20:07:28.926981 +08:00 price: 29.95 title: Pragmatic Version Control id: 1 description: How to use version control new_record: true
从数据库字段构建出来的对象是个hash对象实例,第一个对象属性就是image_url,所以会在这里报错。
至于顺序,跟自定义顺序无关,也不是倒序,是hash对象内部按照key的class_id或hash_code排序吧,我猜的。 wuxu1314: 我刚才测试时也遇到这个问题,您当时的心情我是再明白不过了,感谢您,因为您的分享我也少走了不少弯路,这使我更加愿意和别人分享自己的成功与失败,我以为回报帮助最好的办法就是尽力帮助那些需要帮助的人!
同时另一个问题问您:
ruby test/unit/product_test.rb
DEPRECATION WARNING: model is deprecated and will be removed from Rails 2.0 See http://www.rubyonra
ils.org/deprecation for details. (called from D:/rails/depot/config/../app/controllers/application.r
b:7)
DEPRECATION WARNING: depend_on is deprecated and will be removed from Rails 2.0 See http://www.ruby
onrails.org/deprecation for details. (called from model_without_deprecation at c:/ruby/lib/ruby/gems
/1.8/gems/actionpack-1.13.3/lib/action_controller/deprecated_dependencies.rb:13)
上面两个DEPRECATION WARNING怎样处理,一起学习和享受ruby on rails! hideto: wuxu1314 写道我刚才测试时也遇到这个问题,您当时的心情我是再明白不过了,感谢您,因为您的分享我也少走了不少弯路,这使我更加愿意和别人分享自己的成功与失败,我以为回报帮助最好的办法就是尽力帮助那些需要帮助的人!
同时另一个问题问您:
ruby test/unit/product_test.rb
DEPRECATION WARNING: model is deprecated and will be removed from Rails 2.0 See http://www.rubyonra
ils.org/deprecation for details. (called from D:/rails/depot/config/../app/controllers/application.r
b:7)
DEPRECATION WARNING: depend_on is deprecated and will be removed from Rails 2.0 See http://www.ruby
onrails.org/deprecation for details. (called from model_without_deprecation at c:/ruby/lib/ruby/gems
/1.8/gems/actionpack-1.13.3/lib/action_controller/deprecated_dependencies.rb:13)
上面两个DEPRECATION WARNING怎样处理,一起学习和享受ruby on rails!
deprecated的东西一般都有替代的表述方式的 wuxu1314: hideto 写道wuxu1314 写道我刚才测试时也遇到这个问题,您当时的心情我是再明白不过了,感谢您,因为您的分享我也少走了不少弯路,这使我更加愿意和别人分享自己的成功与失败,我以为回报帮助最好的办法就是尽力帮助那些需要帮助的人!
同时另一个问题问您:
ruby test/unit/product_test.rb
DEPRECATION WARNING: model is deprecated and will be removed from Rails 2.0 See http://www.rubyonra
ils.org/deprecation for details. (called from D:/rails/depot/config/../app/controllers/application.r
b:7)
DEPRECATION WARNING: depend_on is deprecated and will be removed from Rails 2.0 See http://www.ruby
onrails.org/deprecation for details. (called from model_without_deprecation at c:/ruby/lib/ruby/gems
/1.8/gems/actionpack-1.13.3/lib/action_controller/deprecated_dependencies.rb:13)
上面两个DEPRECATION WARNING怎样处理,一起学习和享受ruby on rails!
deprecated的东西一般都有替代的表述方式的今天查看了一下源代码:C:"ruby"lib"ruby"gems"1.8"gems"actionpack-1.13.3"lib"action_controller下deprecated_dependencies.rb中model()的定义如下
# Specifies a variable number of models that this controller depends on. Models are normally Active Record classes or a similar
# backend for modelling entity classes.
def model(*models)
require_dependencies(:model, models)
depend_on(:model, models)
end
deprecate :model
再查看了http://www.rubyonrails.org/deprecation并没有发现替代的方式。