Подтвердить что ты не робот

Как я могу проверить accepts_nested_attributes_for с Rspec Rails3

У меня есть модель следующим образом:

class Greeting < ActiveRecord::Base

  attr_accessible :headline, :icon, :content

  belongs_to :user


  accepts_nested_attributes_for :user, :reject_if => proc { |a| a[:name].blank? || a[:email].blank? } 

Как я могу выполнить тест Rspec для этого?

4b9b3361

Ответ 1

Здесь у вас есть Shoulda макрос для тестирования accepts_nested_attributes_for: http://mediumexposure.com/testing-acceptsnestedattributesfor-shoulda-macros/. Он не поддерживает никаких параметров (например, :reject_if), только голый accepts_nested_attributes_for.

Но для :reject_if вы можете создать допустимую модель приветствия с вложенными атрибутами для User, но без :name. Затем проверьте, сохранен ли пользователь, а затем тот же самый с пустым :email

Итак, вы можете сделать что-то вроде этого:

describe Greeting
  it { expect { Factory(:greeting, :user_attributes => Factory_attributes_for(:user)) }.to change(User, :count).by(1) }
  it { expect { Factory(:greeting, :user_attributes => Factory_attributes_for(:user, :name => '')) }.to_not change(User, :count) }
  it { expect { Factory(:greeting, :user_attributes => Factory.attributes_for(:user, :email => '')) }.to_not change(User, :count) }
end