Search code examples
rspecruby-on-rails-3.2ruby-on-rails-3.1rspec-rails

Rails/RSpec - write rspec testing for Flash Notice


Below is my Helper method for displaying flash notice.

I want to write Rspec Testing so How can I write.

Please Help me.

def flash_type_to_alert(type)
    case type
    when :notice
      return :success
    when :info
      return :info
    when :alert
      return :warning
    when :error
      return :danger
    else
      return type
    end
end

Thanks, In Advance


Solution

  • First, I would improve your helper removing all return statements and removing the case info.

    def flash_type_to_alert(type)
      case type
      when :notice
        :success
      when :alert
        :warning
      when :error
        :danger
      else
        type
      end
    end
    

    The tests you need would be something like that:

    describe "flash_type_to_alert(type)" do
      it "defines the success class when flash notice is set" do
        expect(helper.flash_type_to_alert(:notice)).to eq(:success)
      end
      it "defines the warning class when flash alert is set" do
        expect(helper.flash_type_to_alert(:alert)).to eq(:warning)
      end
      it "defines the danger class when flash error is set" do
        expect(helper.flash_type_to_alert(:error)).to eq(:danger)
      end
      it "defines the given class when any other flash is set" do
        expect(helper.flash_type_to_alert(:info)).to eq(:info)
      end
    end