【发布时间】:2014-01-26 03:17:45
【问题描述】:
我有一个使用“open-uri”的邮件程序。
require 'open-uri'
class NotificationMailer < ActionMailer::Base
def welcome(picasa_picture)
picture = picasa_picture.content.src
filename = picture.split('/').last
attachments.inline[filename] = open(picture).read
mail(
to: 'foo@exmample.com',
from: 'bar@example.com',
subject: 'hi',
)
end
end
但是当我尝试测试该类的任何内容时,我得到了这个错误:
SocketError:
getaddrinfo: nodename nor servname provided, or not known
我发现了这个 SO 帖子:How to rspec mock open-uri 并认为它会有所帮助。我试了一下:
let(:pic_content) { double(:pic_content, src: 'http://www.picasa/asdf/asdf.jpeg') }
let(:picture) { double(:picture, content: pic_content) }
let(:open_uri_mock) { double(:uri_mock, read: true) }
subject { described_class.welcome(picture) }
it 'renders email address of sender' do
subject.stub(:open).and_return(open_uri_mock)
subject.from.should == [ sender_address ]
end
我也尝试了“should_receive”而不是“stub”,但没有帮助。
如何抑制 open-uri 'open' 方法,使其 (1) 不会尝试上网并且 (2) 不会破坏我的测试?
【问题讨论】:
-
我不确定你在这里存根正确的对象的打开方法
-
据我所知,您在调用该方法后正在存根,因此它不起作用
-
这篇文章似乎认为你在内核上做存根:stackoverflow.com/questions/3603256/rspec-how-to-stub-open
标签: ruby-on-rails ruby ruby-on-rails-3 rspec tdd