Skip to content

Instantly share code, notes, and snippets.

@mzahir
Created November 18, 2012 10:39
Show Gist options
  • Save mzahir/4104505 to your computer and use it in GitHub Desktop.
Save mzahir/4104505 to your computer and use it in GitHub Desktop.
[RSpec] Not getting redirect from ProductController
==== products_controller_spec.rb =====
describe ProductsController, "Handling POST for create" do
def sign_in_user
user = FactoryGirl.create(:user)
sign_in user, @user
end
def do_post
post :create, :product => @params
end
before do
@product = mock_model(Product, :save => true)
Product.stub!(:create).and_return(@product)
@params = FactoryGirl.attributes_for(:product)
end
it "should redirect to products after success" do
sign_in_user
do_post
pending "need to figure out why redirect fails"
response.should redirect_to(products_path)
end
end
===== Controller =====
def create
@product = Product.new(params[:product])
if !@product.valid?
flash.now[:error] = ("The product was not added: <br><li>" + @product.errors.full_messages.join("<li>")).html_safe
render "new"
else
if @product.save
redirect_to products_path, :notice => "Product created successfully!"
else
flash.now[:error] = ("There were issues adding the product to the database. Please retry")
render "new"
end
end
end
=== Factory Girl===
FactoryGirl.define do
factory :product do
name "Example Product"
description "This is an example!"
price "9.99"
inventory 1
product_image { File.open(File.join(Rails.root, 'spec', 'support', 'uploads', '1000x450.jpeg')) }
end
end
===== Output from test.log =====
819 (0.0ms) RELEASE SAVEPOINT active_record_1
820 Processing by ProductsController#create as HTML
821 Parameters: {"product"=>{"name"=>"Fruit Mania", "description"=>"The mania that is taking over the planet!", "price"=>"9.99", "inventory"=>"1", "product_image"=>"#<File:0x00
822 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
823 Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE LOWER("products"."name") = LOWER('Fruit Mania') LIMIT 1
824 Completed 200 OK in 8ms (Views: 1.7ms | ActiveRecord: 0.3ms)
825 (0.5ms) rollback transaction
826 (1.0ms) DELETE FROM "products";
827 (0.1ms) DELETE FROM sqlite_sequence where name = 'products';
828 (1.1ms) DELETE FROM "users";
829 (0.1ms) DELETE FROM sqlite_sequence where name = 'users';
===== RSpec Error===
Failures:
1) ProductsController Handling /GET /products (The index action for this controller) should list all products
Failure/Error: Product.should_receive(:index).and_return(@products)
(<Product(id: integer, name: string, description: string, price: decimal, inventory: integer, created_at: datetime, updated_at: datetime, product_image: string) (class)>).index(any args)
expected: 1 time
received: 0 times
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment