require 'test_helper' class BuildersTest < Hermes::IntegrationCase FIXTURE_DATE = Time.parse("Thu, 03 Mar 2011 18:11:26 UTC +00:00").utc fixtures :users context "building models based on fixtures" do test 'creating a model' do user = create_user assert_equal "The Super User", user.name assert_equal FIXTURE_DATE, user.active_at assert user.persisted? end test 'creating a model with bang!' do user = create_user! assert_equal "The Super User", user.name assert_equal FIXTURE_DATE, user.active_at assert user.persisted? end test 'failing to create a model with bang! and invalid attributes' do assert_raise ActiveRecord::RecordInvalid do create_user!(:name => nil) end end test 'do not raise exception when bang is used but not being saved' do assert_nothing_raised do new_user!(:name => nil) end end test 'building a model' do user = new_user assert_equal "The Super User", user.name assert_equal FIXTURE_DATE, user.active_at assert user.new_record? end test 'generating a hash of attributes' do user_attributes = valid_user_attributes assert_equal 'The Super User', user_attributes[:name] assert_equal FIXTURE_DATE, user_attributes[:active_at] end test 'respond_to is valid for attributes' do assert respond_to?(:valid_user_attributes) end test 'respond_to is valid for builder' do assert respond_to?(:create_user) end test 'respond_to accepts include_private argument' do assert !respond_to?(:not_existing_method, true) end test 'blocks are lazily evaluated' do user = new_user assert_not_equal user.counter, new_user.counter end end context "building new models using a specified class" do test 'create an instance of user' do another_user = new_another_user assert_equal 'Another user', another_user.name assert another_user.kind_of?(User) end end context "building new models" do test 'load a model' do user = create_another_user assert_equal 'Another user', user.name assert_equal FIXTURE_DATE, user.active_at end test 'getting its attributes' do user = valid_another_user_attributes assert_equal 'Another user', user[:name] assert_equal FIXTURE_DATE, user[:active_at] end test 'bypass mass-assignment protection' do user = create_another_user(:protected_attr => 1) assert_equal 1, user.protected_attr end test 'respond_to is valid for attributes' do assert respond_to?(:valid_another_user_attributes) end test 'respond_to is valid for builder' do assert respond_to?(:create_another_user) end test 'respond_to is valid for builder with bang' do assert respond_to?(:create_another_user!) end test 'respond_to is valid for builder with new' do assert respond_to?(:new_another_user) end test 'respond_to is not valid for unexistent builder' do assert !respond_to?(:new_invalid_builder_user) end test 'method missing is triggered for inexistent builders' do assert_raise NameError do new_invalid_builder_user end end end test 'build models without blocks' do user = create_blockless_user assert user.is_a?(User) end end