1.使用Rspec,详见http://www.cnblogs.com/fanxiaopeng/p/3563772.html
2.在gemfile中添加
#Gemfile group :development, :test do gem 'factory_girl_rails' end
2.执行bundle install
3,在spec文件夹下新建名叫factories的文件夹,新建一个port.rb文件
FactoryGirl.define do factory :post do name 'Tom' title 'article title' end end
这是在模拟Models中的Port类。
然后在port_scpec.rb中这样使用:
FactoryGirl.create(:post)
这时候遇到一个问题,总是提示错误 ActiveRecord::StatementInvalid: Could not find table Port
方法一:
添加, :class => :Storage
FactoryGirl.define do factory :post, :class => :Post do name 'Tom' title 'article title' end end
方法二,
经过一番搜索,确定原因是因为没有rake,执行以下命令之后,就可以正确跑通了
rake db:migrate db:test:clone
FactoryGirl的其他使用方法请见http://rubyer.me/blog/1460/