Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Product count didn't change by 1

testing create failure

  • Please log in to reply
1 reply to this topic

#1 DvDj

DvDj

    Signalman

  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 02 August 2014 - 11:42 AM

Hi Everyone,

 

So i ran a Rake test on my app and everything returned fine so from that point i went on to add a column to my products table and edited my test accordingly, or so i thought! 

 

Running Rake test again came back with the following error; "Product.count" didn't change by 1.  Expected 4 Actual 3 in the products_controller_test.rb 

 

I tried to research this on the internet and came to the conclusion that it may be my syntax, although for love nor money i can't find one!

 

The products_controller_test.rb is as follows and it is line 27 (test "should create product" do)  that is throwing up the error: -

require 'test_helper'

class ProductsControllerTest < ActionController::TestCase
  setup do
    @product = products(:one)
	@update = {
		title: 'Metal Duo Pen \'Getafe\'', 
		item_code: '030107', 
		colour_description: 'Silver', 
		image_url: 'product_shots/030107.jpg', 
		section: 'writing' 
	}
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:products)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create product" do
    assert_difference('Product.count') do
      post :create, product: @update
    end

    assert_redirected_to product_path(assigns(:product))
  end

  test "should show product" do
    get :show, id: @product
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @product
    assert_response :success
  end

  test "should update product" do
    patch :update, id: @product, product: @update
    assert_redirected_to product_path(assigns(:product))
  end

  test "should destroy product" do
    assert_difference('Product.count', -1) do
      delete :destroy, id: @product
    end

    assert_redirected_to products_path
  end
end

Other relevant code as follows: -

 

/models/product_test.rb

require 'test_helper'

class ProductTest < ActiveSupport::TestCase
	fixtures :products
	test "The following product attributes must not be empty" do 
	product = Product.new 
		assert product.invalid? 
		assert product.errors[:title].any? 
		assert product.errors[:item_code].any? 
		assert product.errors[:colour_description].any?
		assert product.errors[:image_url].any?
		assert product.errors[:section].any?
	end
	
	def new_product(image_url)
		Product.new(title: "My Product Title", 
			item_code: "623505", 
			colour_description: "Red", 
			image_url: image_url, 
			section: "writing")
	end
	
	test "image url" do 
		ok = %w{ fred.gif fred.jpg fred.png FRED.JPG FRED.Jpg http://a.b.c/x/y/z/fred.gif }
		bad = %w{ fred.doc fred.gif/more fred.gif.more }
		ok.each do |name| 
			assert new_product(name).valid?, "#{name} should be valid"
		end
		bad.each do |name| 
			assert new_product(name).invalid?, "#{name} shouldn't be valid" 
		end
	end
	
	test "Item code is not valid if not unique" do 
		product = Product.new(title: "My product title", 
			item_code: products(:ruby).item_code, 
			colour_description: "Red", 
			image_url: "fred.gif", 
			section: "writing")
		assert product.invalid? 
		assert_equal ["has already been taken"], product.errors[:item_code] 
	end
end

my products.yml file

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
  image_url: MyString
  artwork_template_url: MyString
  title: MyString
  item_code: MyString
  description: MyText
  colour_description: MyString
  ind_weight: 1
  carton_qty: 1
  inner_qty: 1
  country_of_origin: MyString
  commodity_code: MyString
  page_number: MyString
  print_area: MyString
  product_size: MyString
  battery_incl: false
  battery_needed: MyString
  certification_url: MyString
  outer_dimensions_length: 1
  outer_dimensions_width: 1
  outer_dimensions_height: 1
  outer_weight: 1
  delivery_time_plain: MyString
  delivery_time_printed: MyString
  packaging: MyString
  print_group: MyString
  product_notes: MyText
  product_logistic_notes: MyText
  discontinued: false
  section: MyString
two:
  image_url: MyString
  artwork_template_url: MyString
  title: MyString
  item_code: MyString
  description: MyText
  colour_description: MyString
  ind_weight: 1
  carton_qty: 1
  inner_qty: 1
  country_of_origin: MyString
  commodity_code: MyString
  page_number: MyString
  print_area: MyString
  product_size: MyString
  battery_incl: false
  battery_needed: MyString
  certification_url: MyString
  outer_dimensions_length: 1
  outer_dimensions_width: 1
  outer_dimensions_height: 1
  outer_weight: 1
  delivery_time_plain: MyString
  delivery_time_printed: MyString
  packaging: MyString
  print_group: MyString
  product_notes: MyText
  product_logistic_notes: MyText
  discontinued: false
  section: MyString
ruby:
  title: Metal Duo pen
  item_code: 030107
  colour_description: Silver
  image_url: product_shots/030107.jpg
  section: writing

And this is the test log, which has an interesting line which says "Unpermitted parameters: section"  Section being the latest column i added.

--------------------------------------------------
ProductsControllerTest: test_should_create_product
--------------------------------------------------
  [1m[35mProduct Load (0.0ms)[0m  SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", 980190962]]
  [1m[36m (0.0ms)[0m  [1mSELECT COUNT(*) FROM "products"[0m
Processing by ProductsController#create as HTML
  Parameters: {"product"=>{"title"=>"Metal Duo Pen 'Getafe'", "item_code"=>"030107", "colour_description"=>"Silver", "image_url"=>"product_shots/030107.jpg", "section"=>"writing"}}
Unpermitted parameters: section
  [1m[35m (0.0ms)[0m  SAVEPOINT active_record_1
  [1m[36mProduct Exists (0.0ms)[0m  [1mSELECT 1 AS one FROM "products" WHERE "products"."item_code" = '030107' LIMIT 1[0m
  [1m[35m (0.0ms)[0m  ROLLBACK TO SAVEPOINT active_record_1
  Rendered products/_form.html.erb (53.9ms)
  Rendered products/new.html.erb within layouts/application (103.6ms)
Completed 200 OK in 210ms (Views: 206.1ms | ActiveRecord: 0.0ms)
  [1m[36m (0.0ms)[0m  [1mSELECT COUNT(*) FROM "products"[0m
  [1m[35m (0.0ms)[0m  rollback transaction
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m

Instead of it being a syntax error i wonder if i have missed something when adding the new column.

 

As always any help will be very much appreciated.



#2 DvDj

DvDj

    Signalman

  • Members
  • 20 posts
  • LocationUnited Kingdom

Posted 05 August 2014 - 02:59 PM   Best Answer

Just in case it helps others I have now resolved the error.  I had missed something in my workflow adding new columns to my Products table.

 

At the very bottom of the products_controller.rb file there is a white-list of permissible columns or product table params.

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:image_url, :artwork_template_url, :title, :item_code, :description, :colour_description, :ind_weight, :carton_qty, :inner_qty, :country_of_origin, :commodity_code, :page_number, :print_area, :product_size, :battery_incl, :battery_needed, :certification_url, :outer_dimensions_length, :outer_dimensions_width, :outer_dimensions_height, :outer_weight, :delivery_time_plain, :delivery_time_printed, :packaging, :print_group, :product_notes, :product_logistic_notes, :discontinued, :section)
    end

Once i added the new columns to this section as well, all tests ran without error.







Also tagged with one or more of these keywords: testing, create, failure

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users