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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:09:31+00:00 2026-05-30T18:09:31+00:00

How do you tweak the arguments that get passed in rspec’s (v 2.6.1) post

  • 0

How do you tweak the arguments that get passed in rspec’s (v 2.6.1) post :create? Pointing me to rspec documentation that explains this command and how to use it would be fine with me. I’ve gone looking but can’t find anything that answers my question.

I’m having difficulty writing rspec to behave properly for my Customer :create action that receives parameters, both for the Customer, as well as a different User object. More precisely, I’m unable to get the values in the hash I pass in the rspec post :create to behave like the input that comes from my form.

Please note that my Customer and User objects are separate ActiveRecords, and each Customer :belongs_to a User.

Here’s my RSpec that’s not doing what I want it to:

describe CustomersController do

  render_views

  describe "for signed-in admins" do

    before(:each) do
      @customer_attr = Factory.attributes_for(:customer)
      @admin = Factory(:admin)
      test_sign_in(@admin.user)
    end

    describe "POST 'create'" do

      describe "success" do

        it "should create a customer" do
          lambda do
            post :create, :customer => @customer_attr # The problem is here
          end.should change(Customer, :count).by(1)
        end
      end
    end
  end
end

Here’s the failure:

1) CustomersController for signed-in admins POST 'create' success should create a customer
  Failure/Error: lambda do
    count should have been changed by 1, but was changed by 0
  # ./spec/controllers/customers_controller_spec.rb:323:in `block (5 levels) in <top (required)>'

The problem (as you’ll see below) is obviously that post :create, :customer => @customer_attr doesn’t include an argument that will end up in params[:user][:email], so the @user.save is failing because params[:user] is empty, but I’ve tried all kinds of ways to recompose the hash and pass it in that line but can’t get it to work.

Here’s the relevant controller code:

def create
  @user = User.new(params[:user])
  @user.user_type = 'customer'

  @customer = Customer.new(params[:customer])

  if @user.save
    @user.customer = @customer
    if @customer.save
      # create stuff
      redirect_to customer_path, :flash => { :success => "Customer created." }
    else
      # failure stuff
    end
  else
    # more failure stuff
  end
end

Here’s the relevant form code (which behaves how I want it to):

<%= form_for(@customer) do |f| %>

  <%= fields_for :user do |user_form| %>
    <%= user_form.label :email %>
    <%= user_form.text_field :email %>
  <% end %>

  <%= f.label :first_name %>
  <%= f.text_field :first_name %>
  <%= f.label :last_name %>
  <%= f.text_field :last_name %>

  <%= f.submit "Create Customer Account" %>
<% end %>

Lastly, here’s my factories code:

factory :user do |f|
  f.sequence :email do |n|
    "person#{n}@example.com"
  end
  f.password                "thepassword"
  f.password_confirmation   "thepassword"
end

factory :customer do |f|
  f.first_name  "First"
  f.last_name   "Last"
  f.user
  after_build { |customer| customer.user.user_type = 'customer' }
end
  • 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-30T18:09:33+00:00Added an answer on May 30, 2026 at 6:09 pm

    The solution I found was to compose my hash to make it look exactly how I wanted, then pass it directly, without the :customer => in front of it.

    before(:each) do
      @customer = Factory(:customer)
      @customer_attr = Factory.attributes_for(:customer)
      @user_attr = {:user => Factory.attributes_for(:user)}
      @customer_and_user_attr = {:customer => @customer_attr }.merge(@user_attr)
    
      @admin = Factory(:admin)
      test_sign_in(@admin.user)
    end
    

    Then in my controller I access items as e.g. params[:customer][:first_name] or params[:user][:email]

    And the rspec looks like:

    post :create, @customer_and_user_attr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello everyone im trying to create this tweak that copies a file from one
How do I tweak this to get yesterday's date using localtime? use strict; sub
So I'm trying to take this post and tweak it for my own purposes
A tweak on this question that I've run into. Consider: class A {}; class
I want to tweak this code from Apple so that instead of using images
Hi I am hoping that somebody can help me to tweak this JQuery code
I'm trying to tweak code that rendered by Glimmer which probably marks my CSS
Is there a preference or tweak that will stop Eclipse from putting up a
Slashdot has a little widget that allows you to tweak your comment threshold to
Help me tweak the CSS of this page: http://draw3cards.com/questions/1501/buyback-vs-flashback I tried to understand why

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.