【问题标题】:How to test message from CanCanCan in Rspec?如何在 Rspec 中测试来自 CanCanCan 的消息?
【发布时间】:2017-04-17 00:34:01
【问题描述】:

我正在寻找测试来自 CanCanCan gem 的消息的方法。

def my
  authorize! :my_words, word, message: I18n.t("messages.login_to_add_new_word")
end

类似的东西?

it "sets flash message" do
  expect(message???).to eq I18n.t("messages.login_to_add_new_word")
end

【问题讨论】:

    标签: rspec cancancan


    【解决方案1】:

    authorize! 方法会引发 CanCanCan::AccessDenied 错误,因此您可以对此进行测试。

    it 'raises error' do
      expect { my }.to raise_error(I18n.t("messages.login_to_add_new_word"))
    end
    

    【讨论】:

    • 我只需要将my 更改为controller.my,因为我在控制器规范中。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多