small medium large xlarge

Image_pragsmall
16 Aug 2016, 14:30
ShangYo Chen (3 posts)

The book wrote:

require 'test_helper'
 
class OrderMailerTest < ActionMailer::TestCase
  test "received" do
    mail = OrderMailer.received(orders(:one))
    assert_equal "Pragmatic Store Order Confirmation", mail.subject
    assert_equal ["dave@example.org"], mail.to
    assert_equal ["depot@example.com"], mail.from
    assert_match /1 x Programming Ruby 1.9/, mail.body.encoded
  end
 
  test "shipped" do
    mail = OrderMailer.shipped(orders(:one))
    assert_equal "Pragmatic Store Order Shipped", mail.subject
    assert_equal ["dave@example.org"], mail.to
    assert_equal ["depot@example.com"], mail.from
    assert_match /<td>1&times;<\/td>\s*<td>Programming Ruby 1.9<\/td>/,
      mail.body.encoded
  end
 
end

The test fails.

I want to point out that the last assert_match is weird because it seems to indicate that orders(:one) is associated with line_items(:two) but it does not.

So I did some fix and the test pass, this is what I do:

  # ...

  setup do
    @order = orders(:one)
    @order.line_items << line_items(:two)
  end

  test "received" do
    mail = OrderMailer.received(@order)
    assert_equal "Pragmatic Store Order Confirmation", mail.subject
    assert_equal ["dave@example.org"], mail.to
    assert_equal ["depot@example.com"], mail.from
    assert_match /1 x Programming Ruby 1.9/, mail.body.encoded
  end

  # ...
Samr_small_pragsmall
16 Aug 2016, 16:22
Sam Ruby (634 posts)

Test passes for me: http://intertwingly.net/projects/AWDwR4/checkdepot-50/section-13.1.html#cmd12

Avatar_pragsmall
02 Apr 2017, 07:10
Dong.Xu (6 posts)

Thank you! Works for me and solved me a 2-days error!


Also, with knowing the reason of this error.

I tried another way to solve the problem.

Update the fixture file line_items.yml

From:

one:
  product: two
  cart: one

two:
  product: ruby
  cart: one

To:

one:
  product: two
  cart: one
  order: one

two:
  product: ruby
  cart: one
  order: one
Generic-user-small
29 Jul 2017, 08:28
James Burchell (1 post)

I had the same error and had to use “Dong.Xu” fix to make it work.

I’m really curious why it doesn’t work..

I have been following the book completely except for the playtime exercises, so was there something there that made this code work? Or is it a bug like in a previous version of Rails (https://forums.pragprog.com/forums/148/topics/4622) I’m running Rails 5.0.2 and Rails 2.3.1p112 on Ubuntu.

You must be logged in to comment