如何测试Rspec中CanCanCan的消息?

问题描述:

我正在寻找测试CanCanCan gem消息的方式。如何测试Rspec中CanCanCan的消息?

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 

authorize!方法引发一个CanCanCan::AccessDenied错误,这样你就可以测试这一点。

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

我只需要将'my'改为'controller.my',因为我在控制器规范中。谢谢。 –