Skip to content

Instantly share code, notes, and snippets.

@RobAWilkinson
Created April 29, 2015 02:12
Show Gist options
  • Save RobAWilkinson/e95908ac3280663c0561 to your computer and use it in GitHub Desktop.
Save RobAWilkinson/e95908ac3280663c0561 to your computer and use it in GitHub Desktop.
Spec_example.rb
require 'rails_helper'
RSpec.describe Dog, :type => :model do
let(:subject) { Dog.new(name: 'Beethoven', nickname: 'The Master', current_shelter: 1, shots: ['rabbies', 'distemper']) }
describe '#name' do
it 'responds to name' do
expect(subject).to respond_to :name
end
it "is invalid without a name" do
subject.name = nil
expect(subject).to be_invalid
end
it "raises an error without a name" do
subject.name = nil
expect{ subject.save! }.to raise_error(ActiveRecord::RecordInvalid)
end
end
describe '#nickname' do
it "responds to a nickname" do
expect(subject).to respond_to :nickname
end
it "is valid without a nickname" do
subject.nickname = nil
expect(subject).to be_valid
end
it "does not raise an error without a nickname" do
subject.nickname = nil
expect{ subject.save! }.to_not raise_error
end
end
describe '#current_shelter' do
it "responds to a current_shelter" do
expect(subject).to respond_to :current_shelter
end
it "is invalid without a shelter" do
subject.current_shelter = nil
expect(subject).to be_invalid
end
it "raises an error without a shelter" do
subject.current_shelter = nil
expect{ subject.save! }.to raise_error(ActiveRecord::RecordInvalid)
end
end
describe '#shots' do
it "should respond with two default shots" do
subject.shots = nil
subject.save
expect( subject.shots.count ).to eq(2)
end
it "should not override shots when assigned on create" do
subject.shots = ['flees']
subject.save
expect( subject.shots.count ).to eq(1)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment