small medium large xlarge

Generic-user-small
04 Mar 2016, 00:51
Dustin King (7 posts)

In iteration G1, the book says to use this test:

	test "should get new" do
		item = LineItem.new
		item.build_cart
		item.product = products(:ruby)
		item.save!
		session[:cart_id] = item.cart.id
		get :new
		assert_response :success
	end

For me that produces this error:

OrdersControllerTest#test_should_get_new:
ActionView::Template::Error: undefined method `title' for nil:NilClass
    app/views/line_items/_line_item.html.erb:8:in `_app_views_line_items__line_item_html_erb___2104287929998029906_70327533065260'
    app/views/carts/_cart.html.erb:4:in `_app_views_carts__cart_html_erb__1337894895523337575_70327524849300'
    app/views/layouts/application.html.erb:37:in `block in _app_views_layouts_application_html_erb__3643981223198127307_70327525243760'
    app/helpers/application_helper.rb:6:in `hidden_div_if'
    app/views/layouts/application.html.erb:36:in `_app_views_layouts_application_html_erb__3643981223198127307_70327525243760'
    test/controllers/orders_controller_test.rb:36:in `block in <class:OrdersControllerTest>'

The stack trace says the error happens in this template:

	<% if line_item == @current_item %>
	<tr id="current_item">
	<% else %>
	<tr>
	<% end %>
		<% logger.debug " ITEM PRODUCT: #{ line_item.product } " %>
		<td><%= line_item.quantity %>&times;</td>
		<td><%= line_item.product.title %></td><!-- <== ERROR IS HERE  -->
		<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
	</tr>

I’m not sure exactly why this error happens, but build_cart is not a way we’ve created carts up to this point, and I don’t know how it’s supposed to work. After trying unsuccessfully to find a way to make the test pass as is, I found that the following version of the unit test passes:

  test "should get new" do
  	cart = Cart.new
  	cart.save!
  	session[:cart_id] = cart.id
  	item = cart.add_product(products(:ruby).id)
	item.save!
  	get :new
  	assert_response :success
  end
Generic-user-small
04 Mar 2016, 02:19
Dustin King (7 posts)

I’m just going to keep adding issues I find to this thread, because I suspect there are going to be a lot of them, and I don’t want to spam the forum.

A little later in Chapter 12, we add a a list of PAYMENT_TYPES to the Order, model to be referenced by the select field in _form.html.erb. The template fails to find this because member variables are private in Ruby. The fix is to add a class-level getter:

class Order < ActiveRecord::Base
	PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order" ]
	
	def self.PAYMENT_TYPES
		PAYMENT_TYPES
	end
end
Generic-user-small
04 Mar 2016, 11:55
Dustin King (7 posts)

As of page 167 in the book (179 in the PDF, iteration G1) I have 100% passing unit tests. The book says to modify line_items.yml

Here’s the before:

one:
  product_id: 
  cart_id: 

two:
  product_id: 
  cart_id: 

Here’s what the book says to make it:

one:
    product: ruby
    order: one
two:
    product: ruby
    cart: one

With this, every single unit test, all 107 of them, throws an error. They all have the same SQLException:

 41) Error:
StoreControllerTest#test_should_not_show_low_store_visits:
ActiveRecord::StatementInvalid: SQLite3::SQLException: table line_items has no column named order: INSERT INTO "line_items" ("order", "created_at", "updated_at", "id", "product_id") VALUES ('one', '2016-03-04 11:45:06', '2016-03-04 11:45:06', 980190962, 207281424)
You must be logged in to comment