zhuliangyu
11/12/2016 - 10:54 PM

rspec

rspec

rails g rspec:install

create a rspect for controller
rails g rspec:controller passwords
create a rspect for model
rails g rspec:model widget



rails g factory_girl:model Car name speed:integer
FactoryGirl.define do
  factory :post do
    title Faker::Lorem.sentence
    body Faker::Lorem.paragraph
  end
end


FactoryGirl.define do
   factory :car do
      sequence :title do |n|
      "title#{n}"
         end
     name "MyString"
     speed 1
   end
end

FactoryGirl.define do
  factory :user do
    # some details
  end

  factory :layout do
    # some details
  end

  factory :feature do
    # some details
  end

  factory :listing do
    headline    'headline'
    home_desc   'this is the home description'
    association :user, factory: :user
    association :layout, factory: :layout
    after(:create) do |liztng|
      FactoryGirl.create_list(:feature, 1, listing: liztng)
    end
  end
end
gem 'rspec-rails', '~> 3.5'
gem "factory_girl_rails", "~> 4.0"
# generate post params for rspec

post(:create, params: {
                          comment: FactoryGirl.attributes_for(:comment),
                          # /ideas/:idea_id/comments
                          idea_id: idea.id  
                      }
    )
describe '#create’ do
    context 'when logged in' do
      it { is_expected.to respond_with 200 }
    end
    context 'when logged out' do
      it { is_expected.to respond_with 401 }
    end
end
include FactoryGirl::Syntax::Methods

#The last line allows us to say things like build(:user) and create(:user) rather than FactoryGirl.build(:user) and FactoryGirl.create(:user).
sequence(:email) do |n|
  SecureRandom.uuid+n.to_s+"@126.COM"
end