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 check the reject_if part with RSpec 2.0.0. I currently have two simple test cases for this.

  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 pretty new to testing, and it looks really weird. Interestingly, is this the correct way to check reject_if? And is there a better way to do this?

+6
ruby-on-rails rspec
source share
1 answer

I see that you want to check reject_if , then the best way to do this is to check it directly:

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

If it is true , then name empty. I think this is a little more eloquent than your code, but not so obvious.

+10
source share

All Articles