关于测试[三]_使用夹具数据

0 views
Skip to first unread message

yuexingfu

unread,
Sep 10, 2007, 1:26:01 AM9/10/07
to rails4scm
在上一节,已经知道如何把夹具数据插入数据库了,本节介绍如何在测试中使用
它。有两种方式:
[1]使用模型类提供的查找方法来读取数据。
[2]Rails定义了一个与之同名的方法,可以通过这个方法来访问预先加载好的模型对象-只要传入YAML夹具中定义的记录名称,该方法返回包含该条
记录数据的模型对象。在这里,调用products(:one)就会得到一个product模型对象。下面是这个例子的代码

require File.dirname(__FILE__) + '/../test_helper'
class ProductTest < Test::Unit::TestCase
fixtures : products
def test_unique_title
product = Product.new(:title => products(:one).title,
:descriptipn => "yyy",
:price => 1,
:image_url => 'fred.gif'
)
Assert !product.save
Assert_equal "has already been
taken",product.errors.on(:title)
end
end

Reply all
Reply to author
Forward
0 new messages