Skip to content

Instantly share code, notes, and snippets.

@lin
Last active August 29, 2015 14:17
Show Gist options
  • Save lin/dd2d0005beac62829d44 to your computer and use it in GitHub Desktop.
Save lin/dd2d0005beac62829d44 to your computer and use it in GitHub Desktop.
Testing

Gem file

group :development, :test do
  gem "rspec-rails"
end

group :test do
  gem "factory_girl_rails"
  gem "capybara"
  gem "guard-rspec"
  gem 'guard-spork'
  gem 'fuubar'
end

rspec

bundle install
rails g rspec:install

guard

guard init rspec
guard start

spork

guard init spork

in Guardfile

guard :rspec, cmd: "bundle exec rspec --drb --format Fuubar" do
  # everything else
enc

1, Generate the test file

rails generate rspec:model User
rails generate rspec:controller Post

specs guides

1, always use expect, not should

expect(it).to do.sth
it.should do.sth
its(:role) { is_expected.to eq(:shopper) }
# don't use
its(:role) { should == :shopper) }

2, describe methods

# .class_method
describe '.active' do
end
# #instance_method
describe '#url' do
end

3, use context when you want to use when

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

documentations

1, locations of file

spec/factories/user.rb

2, simplest example

FactoryGirl.define do
  factory :user do
    email 'example@gmail.com'
    password 'password'
    role 'shopper'
  end
end

3, a little bit complex

FactoryGirl.define do
  factory :zombie do
    name 'Ash'
    graveyard 'Petrosville'

    factory :sally do
      name 'Sally'
      graveyard 'Valley Dim'
    end

    factory :moe do
      name 'Moe'
    end
  end
end

4, keywords from factory girl

subject { create(:user) }
subject { build(:user) }
subject { build_stubbed(:user) }

5, overriding the attributes

subject { build(:user, email: 'another@gmail.com') }

6, faker

gem 'ffaker'

7, sequences at spec/factories/sequences.rb

sequence :email do |n|
  "example_#{n}@gmail.com"
end

Matchers:

describe Zombie do
  it 'includes a tweet' do
    tweet = Tweet.new
    zombie = Zombie.new(tweets: [tweet])
    zombie.tweets.should include(tweet)
  end
end

Expect:

describe Zombie do
  it 'gains 3 IQ points by eating brains' do
    zombie = Zombie.new
    zombie.iq.should == 0
    expect { zombie.eat_brains }.to change { zombie.iq }.by(3)
  end
end

Exceptions:

describe Zombie do
  it 'raises a Zombie::NotSmartEnoughError if not able to make a decision' do
    zombie = Zombie.new
    expect { zombie.make_decision! }.to raise_error(Zombie::NotSmartEnoughError)
  end
end
describe Zombie do
it { should_not be_genius }
end
describe Zombie do
its(:iq) { should == 0 }
end
describe Zombie do
it { should_not be_genius }
its(:iq) { should == 0 }
context "with high iq" do
subject { Zombie.new(iq: 3) }
it { should be_genius }
its(:brains_eaten_count) { should == 1 }
end
end
describe Zombie do
let(:zombie) { Zombie.new(tweets: [tweet]) }
let(:tweet) { Tweet.new }
subject { zombie }
its(:tweets) { should include(tweet) }
it "should have a latest tweet" do
zombie.latest_tweet.should == tweet
end
end
describe Zombie do
context "with high iq" do
let!(:zombie) { Zombie.create(iq: 3, name: 'Anna') }
subject { zombie }
it "should be returned with genius" do
Zombie.genius.should include(zombie)
end
it "should have a genius count of 1" do
Zombie.genius.count.should == 1
end
end
end
describe Zombie do
let(:zombie) { Zombie.new }
subject { zombie }
before { zombie.eat_brains }
it 'is not a dummy zombie' do
zombie.should_not be_dummy
end
it 'is a genius zombie' do
zombie.should be_genius
end
end
describe Zombie do
let(:zombie) { Zombie.new }
subject { zombie }
context 'with a dummy zombie' do
before { zombie.iq = 0 }
it { should be_dummy }
end
context 'with a smart zombie' do
before { zombie.iq = 3 }
it { should_not be_dummy }
end
end
# shared examples
shared_examples_for 'the brainless' do
it { should be_dummy }
it { should_not be_genius }
end
describe Zombie do
let(:zombie) { Zombie.new }
subject { zombie }
it_behaves_like 'the brainless'
end
describe Plant do
let(:plant) { Plant.new }
subject { plant }
it_behaves_like 'the brainless'
end
# focus
describe Zombie do
let(:zombie) { Zombie.new }
subject { zombie }
context 'with a dummy zombie' do
before { zombie.iq = 0 }
it { should be_dummy }
end
context 'with a smart zombie', focus: true do
before { zombie.iq = 3 }
it { should_not be_dummy }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment