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

  • SEARCH
  • Home
  • 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 6137905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:47:05+00:00 2026-05-23T17:47:05+00:00

I’ve got a simple test, pretty much what the scaffold generated, although I can’t

  • 0

I’ve got a simple test, pretty much what the scaffold generated, although I can’t figure out why it’s not working. Here’s the situation:

I’ve got an AttachmentsController:

  # POST /attachments
  # POST /attachments.xml
  def create
    @attachment = Attachment.new(params[:attachment])
    @attachment.idea_id = params[:idea_id]

    respond_to do |format|
      if @attachment.save
        format.html { redirect_to(idea_path(params[:idea_id]), :notice => 'Attachment was successfully created.') }
        format.xml  { render :xml => @attachment, :status => :created, :location => @attachment }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @attachment.errors, :status => :unprocessable_entity }
      end

    end
  end
end

And a spec:

describe AttachmentsController do
  def mock_attachment(stubs={})
    @mock_attachment ||= mock_model(Attachment, stubs).as_null_object
  end

  describe "POST create" do
    describe "with valid params" do
      it "assigns a newly created attachment as @attachment" do
        Attachment.stub(:new).with({'these' => 'params'}) { mock_attachment(:save => true) }
        post :create,:attachment => {'these' => 'params'}
        assigns(:attachment).should be(mock_attachment)
      end

but this (and every other test in this spec) fails with something along the lines of

expected #<Attachment:33902000> => #<Attachment:0x2054db0 @name="Attachment_1001">
     got #<NilClass:4> => nil

Because, for reasons I can’t figure out, AttachmentsController#create isn’t being called.

The route is there:

POST   /attachments(.:format)          {:action=>"create", :controller=>"attachments"}

This is what the log says:

  Processing by AttachmentsController#create as HTML
  Parameters: {"attachment"=>{"these"=>"params"}}
Rendered text template (0.0ms)
Completed 302 Found in 52ms (Views: 23.1ms | ActiveRecord: 0.0ms)

I should also note that I can invoke the create code (and it works great) through the website itself.. it’s just the tests that are failing.

So what would cause post() or get() not to invoke the controller like this??

  • 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-23T17:47:06+00:00Added an answer on May 23, 2026 at 5:47 pm

    You could try should_receive and put it into before block as it’s a better practice:

    describe AttachmentsController do
      describe "POST create" do
        let(:attachment) { mock_attachment(:save => save_result) }
    
        subject { post :create, :attachment => params }
    
        before do
          Attachment.should_receive(:new).and_return(attachment)
        end
    
        describe "with valid params" do
          let(:attachment_params) { {'these' => 'params'} }
          let(:save_result) { true }
    
          it "assigns a newly created attachment as @attachment" do
            assigns(:attachment).should be(mock_attachment)
          end
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string

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.