Skip to content

Instantly share code, notes, and snippets.

@rhysforyou
Created November 28, 2011 03:02
Show Gist options
  • Select an option

  • Save rhysforyou/1398908 to your computer and use it in GitHub Desktop.

Select an option

Save rhysforyou/1398908 to your computer and use it in GitHub Desktop.
class BooksController < ApplicationController
def new
@book = Book.new
end
def create
Book.create(params['book'])
redirect_to books_path
end
def index
@books = Book.all
end
end
require 'spec_helper'
describe BooksController do
before :each do
mock_model("Book")
end
describe "GET 'new'" do
before :each do
get :new
end
it "assigns a new Book to @book" do
assigns(:book).should_not be_nil
assigns(:book).class.should eq(Book)
end
end
describe "POST 'index'" do
before :each do
@attrs = FactoryGirl.attributes_for(:book)
end
it "creates a new Book given valid attributes" do
initial_count = Book.count
post :index, @attrs
(Book.count > initial_count).should be_true
end
end
describe "GET 'index" do
before :each do
10.times { FactoryGirl.create(:book) }
get :index
end
it "assigns an array of Books called @books" do
assigns(:books).should_not be_nil
assigns(:books).class.should eq(Array)
assigns(:books)[0].class.should eq(Book)
end
end
end
› rake spec cucumber
/Users/rpowell93/.rvm/rubies/ruby-1.9.3-p0/bin/ruby -S rspec ./spec/controllers/books_controller_spec.rb ./spec/helpers/books_helper_spec.rb ./spec/models/book_spec.rb ./spec/views/books/index.html.erb_spec.rb ./spec/views/books/new.html.erb_spec.rb
.F....
Failures:
1) BooksController POST 'index' creates a new Book given valid attributes
Failure/Error: (Book.count > initial_count).should be_true
expected false to be true
# ./spec/controllers/books_controller_spec.rb:28:in `block (3 levels) in <top (required)>'
Finished in 3.09 seconds
6 examples, 1 failure
Failed examples:
rspec ./spec/controllers/books_controller_spec.rb:25 # BooksController POST 'index' creates a new Book given valid attributes
rake aborted!
ruby /Users/rpowell93/.rvm/rubies/ruby-1.9.3-p0/bin/ruby -S rspec ./spec/controllers/books_controller_spec.rb ./spec/helpers/books_helper_spec.rb ./spec/models/book_spec.rb ./spec/views/books/index.html.erb_spec.rb ./spec/views/books/new.html.erb_spec.rb failed
Tasks: TOP => spec
(See full trace by running task with --trace)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment