【问题标题】:Rails/RSpec - write rspec testing for Flash NoticeRails/RSpec - 为 Flash Notice 编写 rspec 测试
【发布时间】:2014-03-15 13:02:01
【问题描述】:

下面是我的帮助方法显示闪光通知。

我想写 Rspec 测试所以我该怎么写。

请帮帮我。

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

谢谢,提前

【问题讨论】:

    标签: rspec ruby-on-rails-3.2 ruby-on-rails-3.1 rspec-rails


    【解决方案1】:

    首先,我会改进您的助手,删除所有 return 语句并删除案例 info。

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

    你需要的测试是这样的:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2013-05-11
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多