Created
November 22, 2013 16:05
-
-
Save jkonowitch/7602299 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################### spec/models/episode_spec.rb | |
require 'spec_helper' | |
# here is the original description idea before refactoring; | |
# it works, but can be done in a somewhat cleaner and more Rails-y way below | |
# | |
# describe "the show begins" do | |
# | |
# context "the episode starts" do # FYI, context _is_an_alias_ for describe! they work the same! | |
# before do | |
# @episode = Episode.create(name: "S01-E01") | |
# @episode.start | |
# end | |
# | |
# it "has associated three judges" do | |
# binding.pry | |
# expect(@episode).to have(3).judges | |
# end | |
# it "has associated four chefs" do | |
# expect(@episode).to have(4).chefs | |
# end | |
# end | |
# end | |
describe Episode do | |
subject { Episode.create(name: "S01-E01") } | |
# when we want to signal the beginning of an episode | |
# we can call its start method | |
describe "#start" do | |
it "has associated three judges" do | |
expect(subject.start).to have(3).judges | |
end | |
it "has associated four chefs" do | |
expect(subject.start).to have(4).chefs | |
end | |
end | |
end | |
#################################### app/models/episode.rb | |
class Episode < ActiveRecord::Base | |
validates :name, presence: true | |
has_many :chefs | |
has_and_belongs_to_many :judges | |
def start | |
4.times do | |
self.chefs.create(name: "Ms. Chef") | |
end | |
3.times do | |
self.judges.create(name: "Mr. Judge") | |
end | |
self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment