Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing :reject_if in anaf

I have a user model

class User < ActiveRecord::Base
  has_many :languages, :dependent => :destroy
  accepts_nested_attributes_for :languages,   :reject_if => lambda { |l| l[:name].blank? }
end

I want to test reject_if part with RSpec 2.0.0. Currently I have two simple test cases for that

  it "should not save language without name by accepts_nested_attributes" do
    lambda {
      @user.update_attributes!("languages_attributes"=>{"0"=>{}})
    }.should_not change(Language, :count)
  end

  it "should save language with name by accepts_nested_attributes" do
    lambda {
      @user.update_attributes!("languages_attributes"=>{"0"=>{"name"=>"lang_name"}})
    }.should change(Language, :count).by(1)
  end

However I'm quite new to testing and it looks really weird imho. I wonder if this is a right way to test reject_if? And is there is a nicer way to do that?

like image 280
Tadas T Avatar asked Dec 13 '10 21:12

Tadas T


1 Answers

I see that you're looking to test the reject_if then the best way to do this is to test it directly:

anaf_for_languages = User.nested_attributes_options[:languages]
anaf_for_languages[:reject_if].call({ "name" => "" }).should be_true

If it's true, then name is blank. I think this is a little more succinct than your code, but not as immediately obvious.

like image 96
Ryan Bigg Avatar answered Sep 18 '22 11:09

Ryan Bigg