Rspec on rails

阅读更多
1.Need to install gem: rspec, rspec-rails,
plugin: machinist(to create fake records for test)
rspec-on-rails-matchers(provide method: belong_to has_many for model test)
2.Need to install gem: faker, and write bluepoints.rb in spec directory,like follow

require 'faker'

Sham.name         { Faker::Name.name }
Sham.email        { Faker::Internet.email }
Sham.street_no    { (1..100).map.to_a.rand }
Sham.street_name  { Faker::Address.street_name }
Sham.street_type(:unique => false){ rand(2) == 1 ? 'street' : 'road' }
Sham.age(:unique => false){ (1..100).map.to_a.rand }
Sham.gender(:unique => false){ rand(2) }
Sham.suburb_id(:unique => false){ (1..100).map.to_a.rand }
Sham.birthday do
  Date.civil((1990...2009).to_a.rand,
    (1..12).to_a.rand,
    (1..28).to_a.rand)
end

Person.blueprint do
  home_address    { Address.make }
  work_address    { Address.make }
  name            { Sham.name }
  email           { Sham.email }
  age             { Sham.age }
  birthday        { Sham.birthday }
  gender          { Sham.gender }
end

Address.blueprint do
  Person
  street_no       { Sham.street_no }
  street_name     { Sham.street_name }
  street_type     { Sham.street_type }
  suburb_id       { Sham.suburb_id }
end


Modify spec_helper.rb

require File.expand_path(File.dirname(__FILE__) + "/bluepoints.rb")

see the details
http://toolmantim.com/articles/fixtureless_datas_with_machinist_and_sham

3.Write spec.rb file to test.

你可能感兴趣的:(rspec,Rails)