Skip to content
zkan edited this page Mar 24, 2013 · 15 revisions
  • git checkout -b modeling_registrant
  • create file /spec/models/registrant_spec.rb
require 'spec_helper'

describe Registrant do

  before { @registrant = Registrant.new(name: "Twin Panichsomabt", email: "roofimon@live.com") }

  subject { @registrant }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
end

rails migration guide

  • rails generate migration CreateRegistrants
  • db/migrate/20130323175601_create_registrants.rb
class Registrants < ActiveRecord::Migration
  def change
    create_table :registrants do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end
  • bundle exec rake db:migrate RAILS_ENV='test'
class Registrant < ActiveRecord::Base
  attr_accessible :name, :email
end

Validation

require 'spec_helper'

describe Registrant do

  before { @registrant = Registrant.new(name: 'Twin Panichsomabt', email: 'roofimon@live.com') }

  subject { @registrant }

  it { should respond_to(:name) }
  it { should respond_to(:email) }

  describe 'when name is not present' do
    before { @registrant.name = " " }
    it { should_not be_valid }
  end
end
class Registrant < ActiveRecord::Base 
  attr_accessible :name, :email

  validates :name,  presence: true
end
require 'spec_helper'

describe Registrant do

  before { @registrant = Registrant.new(name: 'Twin Panichsomabt', email: 'roofimon@live.com') }

  subject { @registrant }

  it { should respond_to(:name) }
  it { should respond_to(:email) }

  describe 'when name is not present' do
    before { @registrant.name = ' ' }
    it { should_not be_valid }
  end

  describe 'when email is not present' do
    pending
  end

  describe 'when name is longer than 50 characters' do
    pending
  end

  describe 'when email format is invalid' do
    pending
  end

  describe 'when email format is valid' do
    pending
  end

  describe 'when email address is already taken' do
    pending
  end

end
  • rails generate migration add_index_to_registrants_email
class AddIndexToRegistrantsEmail < ActiveRecord::Migration
  def change
    add_index :registrants, :email, unique: true
  end
end

Clone this wiki locally