ruby on rails 上安装及使用rspec

Jul 23 2010 Published by Tony under Ruby

rspec 是 ruby 中最有名的测试框架。针对于rails,rspec有个很好的插件 rspec-rails 本文介绍这个插件的安装。
1 安装rspec: $ gem install rspec
2 安装rspec-rails:$ gem install rspec-rails
3 在rails目录下运行:$ ruby script/generate rspec
4 Spec::Rails可以生成model rspec 和 restful controller
命令如下:ruby script/generate rspec_scaffold purchase order_id:integer created_at:datetime amount:decimal
同样也可以生成只生成model和rspec:ruby script/generate rspec_model person
或者controller:ruby script/generate rspec_controller person

No responses yet

Ruby on Rails ActiveRecord中Has_many的参数说明

Apr 20 2009 Published by Tony under Ruby

Rails ActiveRecord中Has_many的参数说明

:class_name
Specify the class name of the association. Use it only if that name can‘t be inferred from the association name. So has_many :products will by default be linked to the Product class, but if the real class name is SpecialProduct, you‘ll have to specify it with this option.
:conditions
Specify the conditions that the associated objects must meet in order to be included as a WHERE SQL fragment, such as price > 5 AND name LIKE ‘B%’. Record creations from the association are scoped if a hash is used. has_many :posts, :conditions => {:published => true} will create published posts with @blog.posts.create or @blog.posts.build.
:o rder
Specify the order in which the associated objects are returned as an ORDER BY SQL fragment, such as last_name, first_name DESC.
:foreign_key
Specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and “_id” suffixed. So a Person class that makes a has_many association will use “person_id” as the default :foreign_key.
:primary_key
此项用来设置关联主键,默认为ID。
:dependent
如果此项设置为:destroy那么当删除此对象时,会关联删除many端的所有对象,并且会在删除之前调用destroy方法。如果设置为:delete_all这同样是关联删除,但是不会调用destroy方法。如果设置为:nullify,则会将所有的外键设置为null而不删除。
*注意:* 当启用 :through 选项时,:dependent选项将被忽略.
:finder_sql
Specify a complete SQL statement to fetch the association. This is a good way to go for complex associations that depend on multiple tables. Note: When this option is used, find_in_collection is not added.
:counter_sql
Specify a complete SQL statement to fetch the size of the association. If :finder_sql is specified but not :counter_sql, :counter_sql will be generated by replacing SELECT … FROM with SELECT COUNT(*) FROM.
:extend
Specify a named module for extending the proxy. See “Association extensions”.
:include
Specify second-order associations that should be eager loaded when the collection is loaded.
:group
An attribute name by which the result should be grouped. Uses the GROUP BY SQL-clause.
:having
Combined with +:group+ this can be used to filter the records that a GROUP BY returns. Uses the HAVING SQL-clause.
:limit
An integer determining the limit on the number of rows that should be returned.
:o ffset
An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
:select
By default, this is * as in SELECT * FROM, but can be changed if you, for example, want to do a join but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will raise an error.
:as
Specifies a polymorphic interface (See belongs_to).
:through
Specifies a Join Model through which to perform the query. Options for :class_name and :foreign_key are ignored, as the association uses the source reflection. You can only use a :through query through a belongs_to or has_many association on the join model.
:source
Specifies the source association name used by has_many :through queries. Only use it if the name cannot be inferred from the association. has_many :subscribers, :through => :subscriptions will look for either :subscribers or :subscriber on Subscription, unless a :source is given.
:source_type
Specifies type of the source association used by has_many :through queries where the source association is a polymorphic belongs_to.
:uniq
If true, duplicates will be omitted from the collection. Useful in conjunction with :through.
:readonly
If true, all the associated objects are readonly through the association.
:validate
If false, don‘t validate the associated objects when saving the parent object. true by default.
:autosave
If true, always save any loaded members and destroy members marked for destruction, when saving the parent object. Off by default.

No responses yet

Ruby on Rails单元测试(Unit Test)的数据准备

Apr 20 2009 Published by Tony under Ruby

Ruby on Rails单元测试(Unit Test)的数据准备

一般来说,在运行Rails单元测试(Unit Test)之前,都要进行rake db:test:prepare
如果你想一次性把所有DB table都建立好,也可以用这个:rake environment RAILS_ENV=test db:migrate
虽然这样做不标准,但是比较快捷,前提是你的migrate脚本中没有会导致出问题的脏数据。

No responses yet

Ruby on Rails单元测试(Unit Test)

Apr 20 2009 Published by Tony under Ruby

Ruby on Rails 单元测试(Unit Test)步骤
(MySQL环境)
#首先建立测试数据库
mysqladmin -u root create db_test
#建立数据库结构
rake db:test:prepare
#运行测试
ruby test/unit/product_test.rb

No responses yet