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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-19 11: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 = Cart.new(:children => "", :family_claim => 1)
 assert !cart.valid?
 assert cart.errors.invalid?(:campsite)
 ..............................
end

So, I just wan't to make sure I'm on the right track. Thanks.
05d8aae3736c731bcfc2c2a5c2a5f316?d=identicon&s=25 Rob Lacey (braindeaf)
on 2008-10-19 11: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,
  }
end

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

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?
end

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


I hope this helps.

RobL
Cdcdda30ac4e349c6ffd0819b9e1666f?d=identicon&s=25 Jay Pangmi (jaeezzy)
on 2008-10-19 13:24
> I hope this helps.
>
> RobL

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