From 3cd3c261bdf02b2da5217fa1c469d30f9d92c9a3 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 14 Jan 2013 12:04:12 +0100 Subject: got users controller test to pass - tickets controller test next. --- users/test/support/stub_record_helper.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'users/test/support/stub_record_helper.rb') diff --git a/users/test/support/stub_record_helper.rb b/users/test/support/stub_record_helper.rb index 1be419a..18db5ae 100644 --- a/users/test/support/stub_record_helper.rb +++ b/users/test/support/stub_record_helper.rb @@ -4,10 +4,11 @@ module StubRecordHelper # return the record given. # If no record is given but a hash or nil will create a stub based on # that instead and returns the stub. - def find_record(klass, record_or_method_hash = {}) - record = stub_record(klass, record_or_method_hash) + def find_record(factory) + record = stub_record factory + klass = record.class finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find_by_id - klass.expects(finder).with(record.to_param).returns(record) + klass.expects(finder).with(record.to_param.to_s).returns(record) return record end @@ -17,25 +18,24 @@ module StubRecordHelper # If the second parameter is a record we return the record itself. # This way you can build functions that either take a record or a # method hash to stub from. See find_record for an example. - def stub_record(klass, record_or_method_hash = {}, persisted = true) + def stub_record(factory, record_or_method_hash = {}) if record_or_method_hash && !record_or_method_hash.is_a?(Hash) return record_or_method_hash end - stub record_params_for(klass, record_or_method_hash, persisted) + FactoryGirl.build_stubbed(factory).tap do |record| + record.stubs(record_or_method_hash) if record_or_method_hash.present? + end end - def record_params_for(klass, params = {}, persisted = true) - if klass.respond_to?(:valid_attributes_hash) - params.reverse_merge!(klass.valid_attributes_hash) + # returns deep stringified attributes so they can be compared to + # what the controller receives as params + def record_attributes_for(factory, attribs_hash = nil) + FactoryGirl.attributes_for(factory, attribs_hash).tap do |attribs| + attribs.keys.each do |key| + val = attribs.delete(key) + attribs[key.to_s] = val.is_a?(Hash) ? val.stringify_keys! : val + end end - params[:params] = params.stringify_keys - params.reverse_merge! :id => "A123", - :to_param => "A123", - :class => klass, - :to_key => ['123'], - :to_json => %Q({"stub":"#{klass.name}"}), - :new_record? => !persisted, - :persisted? => persisted end end -- cgit v1.2.3 From a9ef046a596b5876792b1cc4ab0d26b9b255c342 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 14 Jan 2013 16:24:25 +0100 Subject: tickets controller tests passing --- users/test/support/stub_record_helper.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'users/test/support/stub_record_helper.rb') diff --git a/users/test/support/stub_record_helper.rb b/users/test/support/stub_record_helper.rb index 18db5ae..168a827 100644 --- a/users/test/support/stub_record_helper.rb +++ b/users/test/support/stub_record_helper.rb @@ -4,10 +4,11 @@ module StubRecordHelper # return the record given. # If no record is given but a hash or nil will create a stub based on # that instead and returns the stub. - def find_record(factory) - record = stub_record factory + def find_record(factory, attribs_hash = {}) + attribs_hash.reverse_merge!(:id => Random.rand(10000).to_s) + record = stub_record factory, attribs_hash klass = record.class - finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find_by_id + finder = klass.respond_to?(:find_by_param) ? :find_by_param : :find klass.expects(finder).with(record.to_param.to_s).returns(record) return record end @@ -18,11 +19,15 @@ module StubRecordHelper # If the second parameter is a record we return the record itself. # This way you can build functions that either take a record or a # method hash to stub from. See find_record for an example. - def stub_record(factory, record_or_method_hash = {}) + def stub_record(factory, record_or_method_hash = {}, persisted=false) if record_or_method_hash && !record_or_method_hash.is_a?(Hash) return record_or_method_hash end FactoryGirl.build_stubbed(factory).tap do |record| + if persisted or record.persisted? + record_or_method_hash.reverse_merge! :created_at => Time.now, + :updated_at => Time.now, :id => Random.rand(100000).to_s + end record.stubs(record_or_method_hash) if record_or_method_hash.present? end end -- cgit v1.2.3