【发布时间】:2014-08-22 03:40:31
【问题描述】:
我正在尝试通过最后一个测试:尝试引发异常我无法使其工作 我的错误日志:
1) 如果没有任何 RIGHT 匹配,那么 VideoSearch 会发现整个采样器集合失败的抱怨 失败/错误:expect(vs.find_all_by_title(2343)).to raise_error 预期异常,但未引发任何异常
我的代码:
class VideoSearch
attr_accessor :title, :samplers
def initialize(params)
@title = params
@samplers = []
end
def find_all_by_title(title)
return [] if title.blank?
Video.where(title: title).first
end
def find_the_whole_collection(title)
if find_all_by_title(title)
sampler = find_all_by_title(title)
@samplers = [sampler] #add the video as the first sample
else
raise Exception
return false # it's false if there 's not any match!
end
end
end
我的规格:
describe 'find then whole collection of samplers ' do
let(:v1) { v1 = Video.create( title: 2345 ) }
let(:vs) { VideoSearch.new v1.title }
let(:sample) { vs.find_all_by_title(v1.title) }
context 'failing' do
before :each do
vs.stub(:find_all_by_title).and_return(nil)
end
it ' complains if there is not any RIGHT match ' do
expect(vs.find_all_by_title(2343)).to raise_error
end
end
end
【问题讨论】: