开发者

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 chan开发者_开发问答ge(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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜