所以我對Rails非常陌生,並且在RSpec中掙扎了一番,特別是嘲笑某些事情。所以我有一個模塊(關注),我的一些控制器正在使用。該模塊(ValidateAdmin)在current_user和logged_in_user中調用另外兩個函數。爲了覆蓋最多的可能性,我想將這兩個函數存根並設置它們的返回值。有關如何完成這項工作的任何建議?控制器的RSpec嘲諷模塊功能
describe ValidateAdmin do
before :all do
class FakeController < ApplicationController
include ValidateAdmin
end
end
let(:controller) { FakeController.new }
describe '#validate_admin_logged_in' do
controller.stub(:current_user).and_return(instance_double(User, admin?: true))
controller.stub(:logged_in_user).and_return(instance_double(User, admin?: true))
[RSpec contexts go here]
編輯 - 修正它包含我之前的'塊'的模擬語句。也只是實現存根被棄用,所以切換到允許語句。
現在使用你當前的代碼會發生什麼? –
@MohammadAbuShady對不起,沒有把這個問題,我有點匆忙。它拋出了一個非常奇怪的錯誤,儘管我通過將它包含在'before'塊中來解決它。愚蠢的錯誤在我的部分,但感謝您的迴應。 – jdune