module MorseSpecHelpers

Constants

VERSION

Public Instance Methods

acts_as_a_list() click to toggle source
# File lib/morse_spec_helpers.rb, line 9
def acts_as_a_list
  describe 'acts_as_list' do
    describe 'given a blank position on save' do
      it 'should add itself to the list' do
        @instance.position = nil
        @instance.save
        @instance.position.should_not be_nil
      end
    end
  end
end
boolean_default_false(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 21
def boolean_default_false(method)
  exists(method)
  it "should default to false" do
    new_thing=@instance.class.new
    expect(new_thing.send(method)).to be_falsey
  end
end
boolean_default_true(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 29
def boolean_default_true(method)
  exists(method)
  it "should default to true" do
    new_thing=@instance.class.new
    expect(new_thing.send(method)).to be_truthy
  end
end
exists(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 37
def exists(method)
  context "must respond to #{method}" do
    it "should respond to #{method}" do
      expect(@instance).to respond_to(method)
    end
  end
end
exposes(variable) click to toggle source
# File lib/morse_spec_helpers.rb, line 45
def exposes(variable)
  it "exposes variable #{variable}" do
    expect(controller.send(variable)).to_not be_nil
  end
end
mandatory_array(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 51
def mandatory_array(method)
  context "#{method}" do
    exists(method)
    it "should be an Array" do
      expect(@instance.send(method).is_a?(Array)).to be_truthy
    end
  end
end
mandatory_belongs_to(model) click to toggle source
# File lib/morse_spec_helpers.rb, line 60
def mandatory_belongs_to(model)
  context "#{model}" do
    context "where the id is incorrect" do
      before do
        allow(model).to receive(:find_by_id).and_return(nil)
      end
      it "should be invalid" do
        expect(@instance).to_not be_valid
      end
    end
    context "where the id is correct" do
      before do
        allow(model).to receive(:find_by_id).and_return(true)
      end
      it "should be valid" do
        expect(@instance).to be_valid
      end
    end
  end
end
mandatory_date(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 81
def mandatory_date(method)
  context "#{method} is a mandatory date" do
    it "should reject a blank #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
    it "should accept a normal date for #{method}" do
      @instance.send("#{method}=",Date.today)
      expect(@instance).to be_valid
    end
    mandatory_thing(method)
  end
end
mandatory_date_or_default(method, default) click to toggle source
# File lib/morse_spec_helpers.rb, line 95
def mandatory_date_or_default(method, default)
  describe "#{method} is a mandatory date that defaults to a value" do
    context "where it is present" do
      it "should be valid" do
        allow(@instance).to receive(method).and_return(Date.today)
        expect(@instance).to be_valid
      end
    end
    context "where it is not present" do
      it "should set to the default" do
        @instance.send("#{method}=", nil)
        expect(@instance).to be_valid
        expect(@instance.send(method)).to eq(default)
      end
    end
  end
end
mandatory_datetime(method,time=nil) click to toggle source
# File lib/morse_spec_helpers.rb, line 113
def mandatory_datetime(method,time=nil)
  context "#{method} is a mandatory datetime" do
    it "should reject a blank #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
    it "should accept a normal datetime for #{method}" do
      @instance.send("#{method}=",time||Time.now)
      expect(@instance).to be_valid
    end
    mandatory_thing(method)
  end
end
mandatory_email(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 127
def mandatory_email(method)
  context "#{method} is a mandatory email" do
    it "should reject a blank #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
    it "should reject a badly formatted email for #{method}" do
      @instance.send("#{method}=","test@test")
      expect(@instance).to_not be_valid
    end
    it "should accept a properly formatted email for #{method}" do
      @instance.send("#{method}=","test@test.com")
      expect(@instance).to be_valid
    end
    it "should reject a normal string for #{method}" do
      @instance.send("#{method}=","test")
      expect(@instance).to_not be_valid
    end
    mandatory_thing(method)
  end
end
mandatory_float(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 149
def mandatory_float(method)
  context "#{method} is a mandatory float" do
    it "should reject a blank #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
    it "should accept a normal float for #{method}" do
      @instance.send("#{method}=",1.1)
      expect(@instance).to be_valid
    end
    mandatory_thing(method)
  end
end
mandatory_integer(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 163
def mandatory_integer(method)
  context "#{method} is a mandatory integer" do
    it "should reject a blank #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
    it "should accept a normal integer for #{method}" do
      @instance.send("#{method}=",1)
      expect(@instance).to be_valid
    end
    mandatory_thing(method)
  end
end
mandatory_relation(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 177
def mandatory_relation(method)
  context "#{method}" do
    exists(method)
    it "should be an ActiveRecord::Relation" do
      expect(@instance.send(method).is_a?(ActiveRecord::Relation)).to be_truthy
    end
  end
end
mandatory_string(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 186
def mandatory_string(method)
  context "#{method} is a mandatory string" do
    it "should reject a blank #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
    it "should accept a normal string for #{method}" do
      @instance.send("#{method}=","test")
      expect(@instance).to be_valid
    end
    mandatory_thing(method)
  end
end
mandatory_string_from_collection(method,collection) click to toggle source
# File lib/morse_spec_helpers.rb, line 200
def mandatory_string_from_collection(method,collection)
  context "#{method} is a mandatory string" do
    exists(method)
    it "should reject a blank #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
    context "where the value is not within the acceptable options" do
      it "should reject it" do
        @instance.send("#{method}=","zgodnflax")
        expect(@instance).to_not be_valid
      end
    end
    context "where the value is within the acceptable options" do
      it "should accept it" do
        @instance.send("#{method}=",collection.first)
        expect(@instance).to be_valid
      end
    end
  end
end
mandatory_string_with_default(method,default) click to toggle source
# File lib/morse_spec_helpers.rb, line 222
def mandatory_string_with_default(method,default)
  context "#{method} is a mandatory string which defaults to #{default}" do
    it "should set a blank string to #{default}" do
      @instance.send("#{method}=",nil)
      expect(@instance).to be_valid
      expect(@instance.send(method)).to eq(default)
    end
  end
end
mandatory_thing(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 232
def mandatory_thing(method)
  context "#{method} is mandatory" do
    exists(method)
    it "should reject a nil #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
  end
end
mandatory_time(method,time=nil) click to toggle source
# File lib/morse_spec_helpers.rb, line 242
def mandatory_time(method,time=nil)
  context "#{method} is a mandatory time" do
    it "should reject a blank #{method}" do
      @instance.send("#{method}=","")
      expect(@instance).not_to be_valid
    end
    it "should accept a normal time for #{method}" do
      @instance.send("#{method}=",time||Time.now)
      expect(@instance).to be_valid
    end
    mandatory_thing(method)
  end
end
one_date_not_after_the_other(first,second) click to toggle source
# File lib/morse_spec_helpers.rb, line 256
def one_date_not_after_the_other(first,second)
  context "for dates, where both #{first} and #{second} exist" do
    before do
      @first=Date.today-1.week
      @second=Date.today+1.week
    end
    context "where #{first} is before #{second}" do
      before do
        @instance.send("#{first}=",@first)
        @instance.send("#{second}=",@second)
      end
      it "should be valid" do
        expect(@instance).to be_valid
      end
    end
    context "where #{first} is the same as #{second}" do
      before do
        @instance.send("#{first}=",@first)
        @instance.send("#{second}=",@first)
      end
      it "should be valid" do
        expect(@instance).to be_valid
      end
    end
    context "where #{first} is after #{second}" do
      before do
        @instance.send("#{first}=",@second)
        @instance.send("#{second}=",@first)
      end
      it "should not be valid" do
        expect(@instance).to_not be_valid
      end
    end
  end
end
one_datetime_not_after_the_other(first,second) click to toggle source
# File lib/morse_spec_helpers.rb, line 292
def one_datetime_not_after_the_other(first,second)
  context "for datetimes, where both #{first} and #{second} exist" do
    before do
      t=Time.now
      @first=t-1.week
      @second=t+1.week
    end
    one_thing_not_after_the_other(first,second)
  end
end
one_thing_not_after_the_other(first,second) click to toggle source
# File lib/morse_spec_helpers.rb, line 303
def one_thing_not_after_the_other(first,second)
  context "where #{first} is before #{second}" do
    before do
      @instance.send("#{first}=",@first)
      @instance.send("#{second}=",@second)
    end
    it "should be valid" do
      expect(@instance).to be_valid
    end
  end
  context "where #{first} is the same as #{second}" do
    before do
      @instance.send("#{first}=",@first)
      @instance.send("#{second}=",@first)
    end
    it "should be valid" do
      expect(@instance).to be_valid
    end
  end
  context "where #{first} is after #{second}" do
    before do
      @instance.send("#{first}=",@second)
      @instance.send("#{second}=",@first)
    end
    it "should not be valid" do
      expect(@instance).to_not be_valid
    end
  end
end
optional_belongs_to(model) click to toggle source
# File lib/morse_spec_helpers.rb, line 333
def optional_belongs_to(model)
  context "#{model}" do
    let(:model_id){"#{model.to_s.underscore}_id"}
    context "where the id is incorrect" do
      before do
        allow(model).to receive(:find_by_id).and_return(nil)
      end
      it "should set the id to nil" do
        @instance.valid?
        expect(@instance.send(model_id)).to be_nil
      end
      it "should be valid" do
        expect(@instance).to be_valid
      end
    end
    context "where the id is correct" do
      before do
        allow(model).to receive(:where).and_return(model.where("id is not null").first)
      end
      it "should be valid" do
        expect(@instance).to be_valid
      end
    end
  end
end
optional_date(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 359
def optional_date(method)
  context "#{method} is an optional date" do
    exists(method)
    it "should make a blank #{method} nil" do
      @instance.send("#{method}=","")
      expect(@instance).to be_valid
      expect(@instance.send(method)).to be_nil
    end
    it "should accept a normal date for #{method}" do
      @instance.send("#{method}=",Date.today)
      expect(@instance).to be_valid
    end
  end
end
optional_datetime(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 374
def optional_datetime(method)
  context "#{method} is an optional datetime" do
    exists(method)
    it "should make a blank #{method} nil" do
      @instance.send("#{method}=","")
      expect(@instance).to be_valid
      expect(@instance.send(method)).to be_nil
    end
    it "should accept a normal datetime for #{method}" do
      @instance.send("#{method}=",Time.now)
      expect(@instance).to be_valid
    end
  end
end
optional_email(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 389
def optional_email(method)
  context "#{method} is an optional email" do
    it "should make a blank #{method} nil" do
      @instance.send("#{method}=","")
      expect(@instance).to be_valid
      expect(@instance.send(method)).to be_nil
    end
    it "should reject a badly formatted email for #{method}" do
      @instance.send("#{method}=","test@test")
      expect(@instance).to_not be_valid
    end
    it "should accept a properly formatted email for #{method}" do
      @instance.send("#{method}=","test@test.com")
      expect(@instance).to be_valid
    end
    it "should reject a normal string for #{method}" do
      @instance.send("#{method}=","test")
      expect(@instance).to_not be_valid
    end
  end
end
optional_float(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 411
def optional_float(method)
  context "#{method} is an optional float" do
    exists(method)
    it "should make a blank #{method} nil" do
      @instance.send("#{method}=","")
      expect(@instance).to be_valid
      expect(@instance.send(method)).to be_nil
    end
    it "should accept a normal float for #{method}" do
      @instance.send("#{method}=",1.0)
      expect(@instance).to be_valid
    end
  end
end
optional_integer(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 426
def optional_integer(method)
  context "#{method} is an optional integer" do
    exists(method)
    it "should make a blank #{method} nil" do
      @instance.send("#{method}=","")
      expect(@instance).to be_valid
      expect(@instance.send(method)).to be_nil
    end
    it "should accept a normal integer for #{method}" do
      @instance.send("#{method}=",1)
      expect(@instance).to be_valid
    end
  end
end
optional_string(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 456
def optional_string(method)
  context "#{method} is an optional string" do
    exists(method)
    it "should make a blank #{method} nil" do
      @instance.send("#{method}=","")
      expect(@instance).to be_valid
      expect(@instance.send(method)).to be_nil
    end
    it "should accept a normal string for #{method}" do
      @instance.send("#{method}=","test")
      expect(@instance).to be_valid
    end
  end
end
optional_string_from_collection(method,collection) click to toggle source
# File lib/morse_spec_helpers.rb, line 471
def optional_string_from_collection(method,collection)
  context "#{method} is an optional string" do
    exists(method)
    it "should make a blank #{method} nil" do
      @instance.send("#{method}=","")
      expect(@instance).to be_valid
      expect(@instance.send(method)).to be_nil
    end
    context "where the value is not within the acceptable options" do
      it "should not accept it" do
        @instance.send("#{method}=","zgodnflax")
        expect(@instance).to_not be_valid
      end
    end
    context "where the value is within the acceptable options" do
      it "should accept it" do
        @instance.send("#{method}=",collection.first)
        expect(@instance).to be_valid
      end
    end
  end
end
optional_time(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 441
def optional_time(method)
  context "#{method} is an optional time" do
    exists(method)
    it "should make a blank #{method} nil" do
      @instance.send("#{method}=","")
      expect(@instance).to be_valid
      expect(@instance.send(method)).to be_nil
    end
    it "should accept a normal time for #{method}" do
      @instance.send("#{method}=",Time.now)
      expect(@instance).to be_valid
    end
  end
end
processes_attachment_to_attribute(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 494
def processes_attachment_to_attribute(method)
  describe "#{method} processing" do
    describe "accessors" do
      %w{attachment attachment_title attachment_alt attachment_remove}.each do |suffix|
        it "should respond to #{method}_#{suffix}" do
          expect(@instance).to respond_to("#{method}_#{suffix}")
        end
      end
      describe "processing" do
        describe "where all is good" do
          it "should process the #{method} attachment properly" do
            @instance.send("#{method}=",nil)
            @instance.valid?
            expect(@instance.send(method)).to be_nil
            @instance.send("#{method}_attachment=",fixture_file_upload("../support/images/placeholder.png", 'image/png'))
            @instance.send("#{method}_attachment_title=","test")
            @instance.valid?
            expect(@instance.send(method)).to_not be_nil
          end
        end
        describe "where the #{method} attachment has errors" do
          it "should be invalid and add attachment errors to #{method} attachment" do
            @instance.send("#{method}=",nil)
            @instance.valid?
            expect(@instance.send(method)).to be_nil
            @instance.send("#{method}_attachment_title=",nil)
            @instance.send("#{method}_attachment=",fixture_file_upload("../support/images/placeholder.png", 'image/png'))
            expect(@instance.valid?).to_not be_truthy
            expect(@instance.errors[method]).to_not be_empty
            expect(@instance.send(method)).to be_nil
          end
        end
      end
    end
  end
end
processes_multiple_attachments_to_attribute(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 531
def processes_multiple_attachments_to_attribute(method)
  describe "#{method} processing" do
    it "should respond to #{method}" do
      expect(@instance.respond_to?(method)).to be_truthy
    end
    describe "accessors" do
      %w{attachment attachment_title attachment_alt attachment_remove}.each do |suffix|
        it "should respond to #{method.to_s.singularize}_#{suffix}" do
          expect(@instance).to respond_to("#{method.to_s.singularize}_#{suffix}")
        end
      end
      describe "processing" do
        describe "where all is good" do
          it "should process the #{method} attachment properly" do
            @instance.send("#{method}=",[])
            @instance.valid?
            expect(@instance.send(method)).to be_empty
            @instance.send("#{method.to_s.singularize}_attachment=",fixture_file_upload("../support/images/placeholder.png", 'image/png'))
            @instance.send("#{method.to_s.singularize}_attachment_alt=","test")
            @instance.valid?
            expect(@instance.send(method)).to_not be_empty
          end
        end
        describe "where the #{method} attachment has errors" do
          it "should be invalid and add attachment errors to #{method} attachment" do
            @instance.send("#{method}=",[])
            @instance.valid?
            expect(@instance.send(method)).to be_empty
            @instance.send("#{method.to_s.singularize}_attachment_alt=",nil)
            @instance.send("#{method.to_s.singularize}_attachment=",fixture_file_upload("../support/images/placeholder.png", 'image/png'))
            expect(@instance.valid?).to_not be_truthy
            expect(@instance.errors[method]).to_not be_empty
            expect(@instance.send(method)).to be_empty
          end
        end
      end
    end
  end
end
redirects_to(path) click to toggle source
# File lib/morse_spec_helpers.rb, line 571
def redirects_to(path)
  it "should redirect to #{path}" do
    expect(response).to redirect_to(path)
  end
end
returns_200() click to toggle source
# File lib/morse_spec_helpers.rb, line 577
def returns_200
  it "returns http success" do
    expect(response).to be_successful
  end
end
sets_flash(method) click to toggle source
# File lib/morse_spec_helpers.rb, line 583
def sets_flash(method)
  it "should set flash #{method}" do
    expect(flash[method]).to_not be_nil
  end
end
sets_flash_alert() click to toggle source
# File lib/morse_spec_helpers.rb, line 589
def sets_flash_alert
  sets_flash(:alert)
end
sets_flash_error() click to toggle source
# File lib/morse_spec_helpers.rb, line 593
def sets_flash_error
  sets_flash(:error)
end
sets_flash_notice() click to toggle source
# File lib/morse_spec_helpers.rb, line 597
def sets_flash_notice
  sets_flash(:notice)
end
should_be_valid() click to toggle source
# File lib/morse_spec_helpers.rb, line 601
def should_be_valid
  it "should be valid" do
    expect(@instance).to be_valid
  end
end
should_not_be_valid() click to toggle source
# File lib/morse_spec_helpers.rb, line 607
def should_not_be_valid
  it "should not be valid" do
    expect(@instance).to_not be_valid
  end
end
there_can_be_only_one(thing) click to toggle source
# File lib/morse_spec_helpers.rb, line 613
def there_can_be_only_one(thing)
  it "should allow only one #{thing}" do
    model=@instance.class
    model_for_factory_girl=model.to_s.underscore
    thing1=FactoryGirl.create model_for_factory_girl, thing=>true
    thing2=FactoryGirl.create model_for_factory_girl, thing=>false
    thing2.update_attribute(thing,true)
    thing2.save
    thing1.reload
    expect(thing1.send(thing)).to be_falsey
    expect(thing2.send(thing)).to be_truthy
  end
end
view_allow(thing) click to toggle source
# File lib/morse_spec_helpers.rb, line 627
def view_allow(thing)
  if thing.is_a?(Array) || thing.is_a?(ActiveRecord::Relation)
    allow(view).to receive(thing.first.class.name.underscore.pluralize.to_sym).and_return(thing)
  else
    allow(view).to receive(thing.class.name.underscore.to_sym).and_return(thing)
  end
end
view_allow_simple_form_for(thing, f=:f) click to toggle source
# File lib/morse_spec_helpers.rb, line 639
def view_allow_simple_form_for(thing, f=:f)
  form=SimpleForm::FormBuilder.new(thing.class.name, thing, self, {})
  allow(view).to receive(f).and_return(form)
end
view_allow_variable(variable_name, value = '') click to toggle source
# File lib/morse_spec_helpers.rb, line 635
def view_allow_variable(variable_name, value = '')
  allow(view).to receive(variable_name).and_return value
end