Forum: Ruby on Rails how to do unit test whic allows null in the column

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jay P. (Guest)
on 2008-10-19 13:28
Hi, I have a carts table with columns: campsite, arrival_date, nights,
parents, children, family_claim, total. Here children and family_claim
column allows null so even if the value is not passed for these columns
it should be valid entry. How can I achieve this. At the moment I'm
using this way in the cart_test.rb:

def test_invalid_with_empty_attribute
 cart = => "", :family_claim => 1)
 assert !cart.valid?
 assert cart.errors.invalid?(:campsite)

So, I just wan't to make sure I'm on the right track. Thanks.
Rob L. (Guest)
on 2008-10-19 13:41
(Received via mailing list)
Hi Jay,

I quite like this way of working its nice and clean and each test
demonstrates exactly what you expect to be invalid and why

def setup
  @va.lid_attributes = {
    :parents => 1
    :children => 1,
    :family_clam => 1,
    :campsite => 'blah',
    :total => 100,

def test_valid
  cart = Cart.create(@valid_attributes)
  assert cart.valid?

def test_valid_if_children_is_nil
  cart = Cart.create(@valid_attributes.merge(:children => nil)
  assert cart.valid?
  # or
  assert cart.errors.on(:children).nil?

def test_invalid_if_total_is_nil
  cart = Cart.create(@valid_attributes.merge(:total => nil)
  assert ! cart.valid?
  assert cart.errors.on(:total)

I hope this helps.

Jay P. (Guest)
on 2008-10-19 15:24
> I hope this helps.
> RobL

Thanks RobL. It sure did help..
This topic is locked and can not be replied to.