Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting duplicate items when mocking using RSpec and Factory Girl
    primarykey
    data
    text
    <p>When using factory girl and RSpec to test my controller, I'm getting the error:</p> <pre><code>Failures: 1) ProductsController Handling POST for create should redirect to products after success Failure/Error: response.should redirect_to(products_path) Expected response to be a &lt;:redirect&gt;, but was &lt;200&gt; </code></pre> <p>Based on the log file, it looks like the object already exists in the DB (and doesn't meet the redirect clause in the controller), which raises two questions:</p> <ol> <li>Why aren't the calls getting mocked/stubbed? </li> <li>What would a workaround be if I wanted to hit the DB? Using <code>@params = FactoryGirl.build(:product).attributes.except(:id, :created_at, :updated_at)</code> doesn't work either.</li> </ol> <p>Any help would be awesome. Thank you.</p> <p>test.log:</p> <pre><code>Processing by ProductsController#create as HTML Parameters: {"product"=&gt;{"name"=&gt;"Fruit Mania", "description"=&gt;"The mania that is taking over the planet!", "price"=&gt;"9.99", "inventory"=&gt;"1", "product_image"=&gt;"#&lt;File:0x00 User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 Product Exists (0.1ms) SELECT 1 AS one FROM "products" WHERE LOWER("products"."name") = LOWER('Fruit Mania') LIMIT 1 </code></pre> <p>products_controller_spec.rb:</p> <pre><code>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 =&gt; @params end before do @product = mock_model(Product, :save=&gt; 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 </code></pre> <p>Factory:</p> <pre><code>FactoryGirl.define do factory :product do name "Fruit Mania" description "The mania that is taking over the planet!" price "9.99" inventory 1 product_image { File.open(File.join(Rails.root, 'spec', 'support', 'uploads', '1000x450.jpeg')) } end end </code></pre> <p>Controller:</p> <pre><code> def create @product = Product.new(params[:product]) if !@product.valid? flash.now[:error] = ("The product was not added: &lt;br&gt;&lt;li&gt;" + @product.errors.full_messages.join("&lt;li&gt;")).html_safe render "new" else if @product.save redirect_to products_path, :notice =&gt; "Product created successfully!" else flash.now[:error] = ("There were issues adding the product to the database. Please retry") render "new" end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload