Ale*_*pov 8 rspec ruby-on-rails mocking carrierwave
我有一个UserAR模型,当我保存User一个填充值为的实例时remote_avatar_url,Carrierwave会自动下载头像.这里有关于此功能的更多信息.
现在,在我的测试中,我想要存根这种行为.我知道我能做到:
allow_any_instance_of(UserAvatarUploader).to receive(:download!)
Run Code Online (Sandbox Code Playgroud)
但是,rspec-mocks文档不鼓励使用allow/expect_any_instance_of.
在测试中将Carrierwave的这一特定功能存在的正确方法是什么?
PS我已经在测试中禁用了图像处理:
config.enable_processing = false if Rails.env.test?
Run Code Online (Sandbox Code Playgroud)
对我来说,答案是使用webmock gem。它在测试期间阻止出站HTTP连接,并允许您轻松地对响应进行存根。
按照说明设置好gem之后,我将其添加到了测试中:
body_file = File.open(File.expand_path('./spec/fixtures/attachments/sample.jpg'))
stub_request(:get, 'www.thedomainofmyimage.example.net').
to_return(body: body_file, status: 200)
Run Code Online (Sandbox Code Playgroud)
通过CarrierWave的remote_<uploader>_url功能可以像魅力一样工作。