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 8101887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:05:55+00:00 2026-06-05T23:05:55+00:00

I have an Order model, which has_many payments and a checkout Controller. The Controller

  • 0

I have an Order model, which has_many payments and a checkout Controller. The Controller should create a new payment if no payment exits.

private 
# Helper method allows calling from several controller-callbacks.
def add_payment_if_not_exists
  if @order.payments.empty?
    Payment.create(...)
  end
end

And now I want to spec this behaviour in CheckoutControllerSpec

it 'should not add a payment when already added' do
  @order = mock_model(Order)
  @order.payments << mock_model(Payment).as_null_object

  Payment.should_not_receive(:new)
  post :homecoming, @params
end

But this throws

Failure/Error: @order.payments << mock_model(Payment).as_null_object
   Mock "Order_1003" received unexpected message :payments with (no args)

Somehow I still do not grok rspecs stubbing and mocking concept entirely. What am I aoing wrong?

  • 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-06-05T23:05:56+00:00Added an answer on June 5, 2026 at 11:05 pm
    @order = mock_model(Order)
    

    Here you create a mock object, just a blank canvas. If you want to set stuff, you should tell it to respond to :payments with something. In this case, an array with 1 entry (a mock Payment object)

    @order.stub(:payments) { [mock_model(Payment).as_null_object]}
    

    That will get you further down the road. Other notes:

    • I’m not sure that this expectation Payment.should_not_receive(:new) tests your behavior. You actually call Payment.create, not Payment.new
    • I expect that you are stubbing out Order.find somewhere to return @order, but that’s not shown here, right?
    • I would normally simplify your @order to the following:

    Here we say that #payments returns a non-empty array, and we don’t care about the contents.

    @order = mock_model(Order)
    @order.stub(:payments) { [stub] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Once again, I have my form_for for my Order model, which has_many :services, :through
I have question model which has many options. In my question controller new action
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have a course model, which has_many timeslots. In the courses model, I have
I have an Invoice model which belongs to Customer (and of course Customer has_many
I currently have a Order and image model which look like this: class Order
I have a data model that represents a weekly order. Eg. my order might
I use Acceleo in order to generate code with a model I have made.
I have three models linked in this manner: Item->Order->Payment Order hasMany Item Order hasOne
I have a Model which I am using to track permissions in a hierarchical

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.