David Piegza
2012-Jun-20 10:18 UTC
[rspec-users] Mock UploadedFile object in controller params
Hi, I''m trying to mock an UploadedFile object and pass it to a controller action. Unfortunately, the mocked object gets stringified in the params hash, so I''m not able to use the mock object in a test. Is there any way to avoid this stringification for UploadedFile objects? This has been discussed already on github: https://github.com/rails/rails/pull/1203 As described, the params are stringified, but it should exclude Rack::Test::UploadedFile: https://github.com/rails/rails/pull/1203#issuecomment-1217081 Here is a small example: let(:file) { mock(''UploadedFile'') } it "tests something" do file.stub(:content_type).and_return ''text/plain'' # this won''t work post :upload, { file: file } # params in controller will be "file" => "#[RSpec::Mock ... ]" end I get a NoMethodError: undefined method `content_type'' for "#[RSpec::Mocks::Mock:0x3fe4723a81bc @name=\"file\"]":String So, how can I test this? Thanks, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120620/571ae269/attachment.html>
Justin Ko
2012-Jun-21 11:14 UTC
[rspec-users] Mock UploadedFile object in controller params
On Jun 20, 2012, at 4:18 AM, David Piegza wrote:> Hi, > > I''m trying to mock an UploadedFile object and pass it to a controller action. Unfortunately, the mocked object gets stringified in the params hash, so I''m not able to use the mock object in a test. > > Is there any way to avoid this stringification for UploadedFile objects? > > This has been discussed already on github: https://github.com/rails/rails/pull/1203 > > As described, the params are stringified, but it should exclude Rack::Test::UploadedFile: https://github.com/rails/rails/pull/1203#issuecomment-1217081 > > Here is a small example: > > let(:file) { mock(''UploadedFile'') } > > it "tests something" do > file.stub(:content_type).and_return ''text/plain'' # this won''t work > post :upload, { file: file } # params in controller will be "file" => "#[RSpec::Mock ... ]" > end > > I get a NoMethodError: undefined method `content_type'' for "#[RSpec::Mocks::Mock:0x3fe4723a81bc @name=\"file\"]":String > > So, how can I test this? > > Thanks, > David > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersRails has a built-in helper method for this: http://api.rubyonrails.org/classes/ActionDispatch/TestProcess.html#method-i-fixture_file_upload -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120621/b5c1b1df/attachment.html>
David Piegza
2012-Jun-21 14:45 UTC
[rspec-users] Mock UploadedFile object in controller params
Thanks for the hint. But I would like to mock the UploadedFile object (I don''t want to load a file..). My current workaround is to stub the params hash: controller.stub(:params).and_return { file: file } Would be nice if I could just pass in the mocked UploadedFile object... On 21.06.2012, at 13:14, Justin Ko wrote:> > On Jun 20, 2012, at 4:18 AM, David Piegza wrote: > >> Hi, >> >> I''m trying to mock an UploadedFile object and pass it to a controller action. Unfortunately, the mocked object gets stringified in the params hash, so I''m not able to use the mock object in a test. >> >> Is there any way to avoid this stringification for UploadedFile objects? >> >> This has been discussed already on github: https://github.com/rails/rails/pull/1203 >> >> As described, the params are stringified, but it should exclude Rack::Test::UploadedFile: https://github.com/rails/rails/pull/1203#issuecomment-1217081 >> >> Here is a small example: >> >> let(:file) { mock(''UploadedFile'') } >> >> it "tests something" do >> file.stub(:content_type).and_return ''text/plain'' # this won''t work >> post :upload, { file: file } # params in controller will be "file" => "#[RSpec::Mock ... ]" >> end >> >> I get a NoMethodError: undefined method `content_type'' for "#[RSpec::Mocks::Mock:0x3fe4723a81bc @name=\"file\"]":String >> >> So, how can I test this? >> >> Thanks, >> David >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > > Rails has a built-in helper method for this: > http://api.rubyonrails.org/classes/ActionDispatch/TestProcess.html#method-i-fixture_file_upload > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120621/7ee4420c/attachment-0001.html>