From 6f0bd98776f6991c03967faaff48de312aa4d06d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20=C5=A0imi=C4=87?= Date: Fri, 24 May 2013 18:35:51 +0200 Subject: [PATCH] add 'be_normalized_to' matcher for testing params normalization --- .../adapters/cloudmailin_adapter_spec.rb | 13 ++++---- .../adapters/mandrill_adapter_spec.rb | 19 +++++------ .../adapters/postmark_adapter_spec.rb | 33 +++++++++---------- spec/spec_helper.rb | 24 ++++++++++++++ 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/spec/griddler/adapters/cloudmailin_adapter_spec.rb b/spec/griddler/adapters/cloudmailin_adapter_spec.rb index fbe4dea9..86703027 100644 --- a/spec/griddler/adapters/cloudmailin_adapter_spec.rb +++ b/spec/griddler/adapters/cloudmailin_adapter_spec.rb @@ -2,13 +2,12 @@ describe Griddler::Adapters::CloudmailinAdapter, '.normalize_params' do it 'normalizes parameters' do - params = default_params - - normalized_params = Griddler::Adapters::CloudmailinAdapter.normalize_params(params) - normalized_params[:to].should eq ['Some Identifier '] - normalized_params[:from].should eq 'Joe User ' - normalized_params[:subject].should eq 'Re: [ThisApp] That thing' - normalized_params[:text].should include('Dear bob') + Griddler::Adapters::CloudmailinAdapter.normalize_params(default_params).should be_normalized_to({ + to: ['Some Identifier '], + from: 'Joe User ', + subject: 'Re: [ThisApp] That thing', + text: /Dear bob/ + }) end it 'passes the received array of files' do diff --git a/spec/griddler/adapters/mandrill_adapter_spec.rb b/spec/griddler/adapters/mandrill_adapter_spec.rb index ac870352..d2b962e7 100644 --- a/spec/griddler/adapters/mandrill_adapter_spec.rb +++ b/spec/griddler/adapters/mandrill_adapter_spec.rb @@ -2,16 +2,15 @@ describe Griddler::Adapters::MandrillAdapter, '.normalize_params' do it 'normalizes parameters' do - params = default_params - - normalized_params = Griddler::Adapters::MandrillAdapter.normalize_params(params) - normalized_params.each do |params| - params[:to].should eq ['The Token '] - params[:from].should eq 'hernan@example.com' - params[:subject].should eq 'hello' - params[:text].should include('Dear bob') - params[:html].should include('

Dear bob

') - params[:raw_body].should include('raw') + Griddler::Adapters::MandrillAdapter.normalize_params(default_params).each do |params| + params.should be_normalized_to({ + to: ['The Token '], + from: 'hernan@example.com', + subject: 'hello', + text: %r{Dear bob}, + html: %r{

Dear bob

}, + raw_body: %r{raw} + }) end end diff --git a/spec/griddler/adapters/postmark_adapter_spec.rb b/spec/griddler/adapters/postmark_adapter_spec.rb index 2b7120a9..8a11913c 100644 --- a/spec/griddler/adapters/postmark_adapter_spec.rb +++ b/spec/griddler/adapters/postmark_adapter_spec.rb @@ -2,14 +2,13 @@ describe Griddler::Adapters::PostmarkAdapter, '.normalize_params' do it 'normalizes parameters' do - params = default_params - - normalized_params = Griddler::Adapters::PostmarkAdapter.normalize_params(params) - normalized_params[:to].should eq ['Robert Paulson '] - normalized_params[:from].should eq 'tdurden@example.com' - normalized_params[:subject].should eq 'Reminder: First and Second Rule' - normalized_params[:text].should include('Dear bob') - normalized_params[:html].should include('

Dear bob

') + Griddler::Adapters::PostmarkAdapter.normalize_params(default_params).should be_normalized_to({ + to: ['Robert Paulson '], + from: 'tdurden@example.com', + subject: 'Reminder: First and Second Rule', + text: /Dear bob/, + html: %r{

Dear bob

} + }) end it 'passes the received array of files' do @@ -35,16 +34,14 @@ end it 'gets rid of the original postmark params' do - params = default_params - - normalized_params = Griddler::Adapters::PostmarkAdapter.normalize_params(params) - - normalized_params[:ToFull].should be_nil - normalized_params[:FromFull].should be_nil - normalized_params[:Subject].should be_nil - normalized_params[:TextBody].should be_nil - normalized_params[:HtmlBody].should be_nil - normalized_params[:Attachments].should be_nil + Griddler::Adapters::PostmarkAdapter.normalize_params(default_params).should be_normalized_to({ + ToFull: nil, + FromFull: nil, + Subject: nil, + TextBody: nil, + HtmlBody: nil, + Attachments: nil + }) end def default_params diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6d088efd..3f57bdd8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,3 +8,27 @@ config.infer_base_class_for_anonymous_controllers = false config.order = "random" end + +RSpec::Matchers.define :be_normalized_to do |expected| + failure_message_for_should do |actual| + message = "" + expected.each do |k, v| + message << "expected :#{k} to be normalized to #{expected[k].inspect}, "\ + "but received #{actual[k].inspect}\n" unless actual[k] == expected[k] + end + message + end + + description do + "be normalized to #{expected}" + end + + match do |actual| + expected.each do |k, v| + case v + when Regexp then actual[k].should =~ v + else actual[k].should === v + end + end + end +end