class MyController < ApplicationController
MyHelper.sometimes_true_sometimes_false
end
MyHelper.should_receive(:sometimes_true_sometimes_false).and_return true
模块
module MyHelper
def sometimes_true_sometimes_false
[true, false].sample
end
end
控制器
class MyController < ApplicationController
def myaction
if MyHelper.sometimes_true_sometimes_false
@message = "Congratualtions, it's true"
else
@message = "Sorry, it's false"
end
end
end
规范
describe 'GET #myaction'
subject { get :myaction }
context 'when it\'s true' do
before do
MyHelper.should_receive(:sometimes_true_sometimes_false).and_return true
end
specify { expect(assigns(:message)).to eq "Congratulations, it's true" }
end
context 'when it\'s false' do
before do
MyHelper.should_receive(:sometimes_true_sometimes_false).and_return false
end
specify { expect(assigns(:message)).to eq "Sorry, it's true" }
end
end
class MyController < ApplicationController
include MyHelper
sometimes_true_sometimes_false
end
控制器
class MyController < ApplicationController
include MyHelper
def myaction
if sometimes_true_sometimes_false
@message = "Congratualtions, it's true"
else
@message = "Sorry, it's false"
end
end
end
答案 0 :(得分:0)
实际上非常简单,在我创建答案的同时想到了答案(并且使用标题标签进行了精神处理):
MyController.any_instance.should_receive(:sometimes_true_sometimes_false).and_return true
规范
describe 'GET #myaction'
subject { get :myaction }
context 'when it\'s true' do
before do
MyController.any_instance.should_receive(:sometimes_true_sometimes_false).and_return true
end
specify { expect(assigns(:message)).to eq "Congratulations, it's true" }
end
context 'when it\'s false' do
before do
MyController.any_instance.should_receive(:sometimes_true_sometimes_false).and_return false
end
specify { expect(assigns(:message)).to eq "Sorry, it's true" }
end
end