From cdc9629d43632226a08f89b5a0273ae797b45a08 Mon Sep 17 00:00:00 2001 From: Stephen McGinty Date: Wed, 10 Aug 2016 20:51:15 +0100 Subject: [PATCH] Fixed/improved Factory.debug test --- Gemfile | 1 + Gemfile.lock | 18 +++++++++++++++++- spec/cranky_spec.rb | 18 +++++++----------- spec/spec_helper.rb | 22 ++++++++-------------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/Gemfile b/Gemfile index 0b10259..b6d35d3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ source "http://gemcutter.org" group :development do + gem "activemodel" gem "rake" gem "gemcutter" end diff --git a/Gemfile.lock b/Gemfile.lock index cce22e1..e1b59f3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,10 +1,22 @@ GEM remote: http://gemcutter.org/ specs: + activemodel (4.2.6) + activesupport (= 4.2.6) + builder (~> 3.1) + activesupport (4.2.6) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + builder (3.2.2) diff-lcs (1.1.2) docile (1.1.5) gemcutter (0.7.0) + i18n (0.7.0) json (1.8.3) + minitest (5.9.0) rake (0.9.2) rspec (2.6.0) rspec-core (~> 2.6.0) @@ -19,15 +31,19 @@ GEM json (~> 1.8) simplecov-html (~> 0.10.0) simplecov-html (0.10.0) + thread_safe (0.3.5) + tzinfo (1.2.2) + thread_safe (~> 0.1) PLATFORMS ruby DEPENDENCIES + activemodel gemcutter rake rspec simplecov BUNDLED WITH - 1.12.2 + 1.12.5 diff --git a/spec/cranky_spec.rb b/spec/cranky_spec.rb index df7b94d..3ff4e50 100644 --- a/spec/cranky_spec.rb +++ b/spec/cranky_spec.rb @@ -79,19 +79,15 @@ describe "debugger" do it "raises an error if the factory produces an invalid object when enabled (rails only)" do - error = false - begin - Factory.debug(:user, :valid => false) - rescue - error = true - end - error.should == true + expect { Factory.debug(:user) }.to raise_error( + 'Oops, the User created by the Factory has the following errors: {:required_attr=>["can\'t be blank"]}' + ) end it "debug works like build and create when there are no errors" do - Factory.debug(:user).class.should == User - Factory.debug(:user).saved?.should == false - Factory.debug!(:user).saved?.should == true + Factory.debug(:user, required_attr: true).class.should == User + Factory.debug(:user, required_attr: true).saved?.should == false + Factory.debug!(:user, required_attr: true).saved?.should == true end end @@ -151,7 +147,7 @@ end it "returns nothing extra in the attributes" do - crank(:user_attrs).size.should == 6 + crank(:user_attrs).size.should == 5 end specify "attributes for works with factory methods using inherit" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 24acdc5..0c6e73b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,13 +2,18 @@ SimpleCov.start require 'cranky' +require 'active_model' RSpec.configure do |config| config.expect_with(:rspec) { |c| c.syntax = :should } end class TestClass - attr_accessor :valid + include ActiveModel::Validations + + attr_accessor :required_attr + + validates_presence_of :required_attr def save @saved = true @@ -18,14 +23,6 @@ def saved? !!@saved end - def valid? - @valid - end - - def errors - "some validation errors" - end - def attributes self.instance_variables end @@ -59,7 +56,6 @@ def user_manually u.unique = "value#{n}" u.email = "fred@home.com" u.address = Factory.build(:address) - u.valid = true u end @@ -69,8 +65,7 @@ def user_by_define :role => :user, :unique => "value#{n}", :email => "fred@home.com", - :address => Factory.create(:address), - :valid => true + :address => Factory.create(:address) u.argument_received = true if options[:argument_supplied] u end @@ -86,8 +81,7 @@ def admin_by_define def address define :address => "25 Wisteria Lane", - :city => "New York", - :valid => true + :city => "New York" end def user_hash