Skip to content

Instantly share code, notes, and snippets.

@styliii
Created November 6, 2012 20:15
Show Gist options
  • Save styliii/4027227 to your computer and use it in GitHub Desktop.
Save styliii/4027227 to your computer and use it in GitHub Desktop.
test example
# 11.6.12
# Jack Dorsey - creator of Twitter
# gem 'rspec'
# bundle
# rails g rspec: install
# in spec, create new folder, models
# rails g test_unit: model Song #=> didn't work as expected
# create songs_spec.rb
require 'spec_helper'
describe Song do
context '.add_genre' do
let(:song){Song.create(:name = "Thriller")}
let(:genre){Genre.create(:name => "Pop")}
it "should add a genre" do
song.add_genre(genre)
song.genres.should include(genre)
end
it "should return the newly added genre" do
song.add_genre(genre).should ==genre
end
it "should accept a genre name" do
song.add_genre("Pop")
song.genres.find_by_name("Pop").should be_true`
end
end
rake spec
def add_genre(genre)
genre = Genre.create(:name => genre) if genre.is_str?
self.song_genres.create(:genre => genre).genre
end
# each test is run independently in rspec
# anything that happens in a transaction are dependent on each other. therefore, it begins a transaction,
# let's say inserts into songs, and genres, but then it fails
# so it rollsback from where it began
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment