I'm writing a system test for an app that uses active storage. The view I'm testing includes presigned urls to objects on amazon s3. I'd obviously like to avoid sending GET requests to s3 in my tests. Instead, I'd like to "stub" active storage so that the urls for my attachments all point to one particular file on my test machine ("/test_data/placeholder.jpg"), rather than to s3.
My view code uses url_for to generate s3 urls. How can I stub url_for to return my one specific desired url?
Excerpts of my failed attempt follow.
In my view (user.jbuilder.json):
json.user_thumbnail = url_for @user.photo.variant(resize: "100x100")
In my tests:
class UserPageTest < ApplicationSystemTestCase
test "thumbnail appears on user page" do
User.any_instance.stubs(:photo).returns(stub(:url => "/test_data/placeholder_thumbnail.jpg") ))
visit '/user_profile/123'
end
end
When I run the test I get this error:
2021-11-04 20:26:40 -0400 Rack app ("GET /user/123.json" - (127.0.0.1)): #<Minitest::Assertion: unexpected invocation: #<Mock:0x7ff448c52af8>.to_model()