加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 测试:在anaf中的reject_if

发布时间:2020-12-16 23:29:06 所属栏目:百科 来源:网络整理
导读:我有一个用户模型 class User ActiveRecord::Base has_many :languages,:dependent = :destroy accepts_nested_attributes_for :languages,:reject_if = lambda { |l| l[:name].blank? }end 我想用RSpec 2.0.0测试reject_if部分.目前我有两个简单的测试用例
我有一个用户模型
class User < ActiveRecord::Base
  has_many :languages,:dependent => :destroy
  accepts_nested_attributes_for :languages,:reject_if => lambda { |l| l[:name].blank? }
end

我想用RSpec 2.0.0测试reject_if部分.目前我有两个简单的测试用例

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

但是我对测试很新,看起来很奇怪.
我想知道这是否是测试reject_if的正确方法?有没有更好的方法呢?

解决方法

我看到你要测试reject_if然后最好的方法是直接测试它:
anaf_for_languages = User.nested_attributes_options[:languages]
anaf_for_languages[:reject_if].call({ "name" => "" }).should be_true

如果是,则名称为空.我认为这比你的代码更简洁,但不是那么明显.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读