Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 314515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:12:25+00:00 2026-05-12T08:12:25+00:00

This is something that has been driving me mad over the past few days.

  • 0

This is something that has been driving me mad over the past few days. I have an action which allows authenticated users to upload assets to the site. I know that the controller action is correct as I can run through the process manually however I want to test it using rspec.

I have to use the request helper so I can reuse an authenticated session which is a :given for this set of tests.

it "should allow authenticated file uploads" do
    file = File.open(a_valid_file)
    mock_file = mock("file")
    mock_file.stub!(:path).and_return(file.path)

    request( resource(:assets), :method => "POST",
        :params => { :file => 
            {:tempfile => mock_file, :filename => File.basename(file.path)} }
    )
end

If I breakpoint inside the spec it all works nicely, however when I run the spec and try to access the path in the controller action through the debugger I get this:

e file[:tempfile].path
NoMethodError Exception: undefined method `path' for "#[Spec::Mocks::Mock:0x3fda2a4736c0 @name=\"file\"]":String

My guess is that the stub!(:path) is not being set for whatever mock object is making it through the request.

The question is: Am I going about the right way for testing file uploads and if not what is another way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:12:25+00:00Added an answer on May 12, 2026 at 8:12 am

    I was doing it wrong. By using request it was calling to_s on all paramaters, so my mock object was being passed as “#[Spec::Mocks::Mock:0x3fda2a4736c0 @name=\”file\”]”. That will teach me to pay more attention to exception output.

    Instead I should use multipart_post and stub out the authentication calls in a block.

    it "should allow authenticated file uploads" do
        file = File.open(a_valid_file)
    
        multipart_post( resource(:assets), :method => "POST", 
                :params => { :file => file } ) do |controller|
            controller.stub!(:ensure_authenticated).and_return(true)
            controller.session.stub!(:user).and_return(User.first)
    
        )
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been driving me crazy for the past few days. I've done something
I've been driving myself crazy over the past few days over this one. We've
This has been driving me mad for weeks.I have a variable data that I
This has been driving me crazy the past couple of days. I'm trying to
I have a problem that has been driving me nuts for days.. I've tried
This has been driving me nuts for the past hour. I have two computers,
I know this is something that has been discussed over and over, and I
This is something that I have had trouble with in the past and it's
So this is something that I have not been able to find any documentation
This is something that has been pulling at me for a while. Consider a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.