Integration Testing

PEZ says:

I can't get my test to run past assert_template "index". Here's what that part of the test looks like. Note that I've done the "play time" exercise for making the checkout us only one action, which is why I do post_via_redirect "/store/checkout".

post_via_redirect "/store/checkout", 
  :order => { :name => "Dave Thomas", 
    :address => "123 The Street", 
    :email => "dave@pragprog.com", 
    :pay_type => "check" }
assert_response :success 
assert_template "index" 
assert_equal 0, session[:cart].items.size

The failure message is clear enough: expecting <"index"> but rendering with <"checkout">

But it isn't very helpful. I can't figure out why the test doesn't make it to the store index. Running the application manually in the browser works. Anyone has any tips on how to get some more information out? Some other asserts I can add?

sucellus says:

I'm having the same problem as PEZ here. I'm wondering if it has something to do with how the "order" parameters are getting passed to the checkout method. If the checkout method isn't correctly identifying that the order is populated then it will redirect back to checkout.

PEZ adds:

OK, so this is really bothering me. There's a difference between the "manual" browser usage and the automated testing environment that I can't seem to figure out. I tried to implement the payment types as a lookup table as discussed here. I came to the conclusion that initializing constants from the database might bite me if I can't make sure the test database is initialized before the PaymentType class is defined. I'd like to figure out how to do that, but in lacking that I changed it so that now I use class methods instead, just to get the tests to run. Like so:

class PaymentType < ActiveRecord::Base
  validates_presence_of :disp
  validates_presence_of :value
 
  def self.options
    self.find(:all).collect {|pt| [pt.disp, pt.value]}
  end
 
  def self.values
    self.options.map {|disp, value| value}
  end
end

My model validates pay_type this way:

  validates_inclusion_of :pay_type, :in => PaymentType.values

And the view uses PaymentType.options to populate the select list. Then my test have this:

  def setup 
    ...
    PaymentType.create(:disp => "Check", :value => "check")
    PaymentType.create(:disp => "Creditcard", :value => "cc")
  end

To my surprise I still can't get the functional tests to get past the validation. Works when exercising the application. But not in testing. The only way I can get the tests to pass is by either removing the pay_type validation or hard coding the types into a variable. Any ideas why this is so?

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License