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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:01:52+00:00 2026-06-16T21:01:52+00:00

I am testing an Invoice model (a Client has many invoices, an Invoice belongs

  • 0

I am testing an Invoice model (a Client has many invoices, an Invoice belongs to a Client) and trying to check whether the create method works.

This is what I have come up with:

before do
  @valid_invoice = FactoryGirl.create(:invoice)
  @valid_client = @valid_invoice.client
end

it "creates a new Invoice" do
    expect {
      post :create, { invoice: @valid_client.invoices.build(valid_attributes), client_id: @valid_client.to_param }
    }.to change(Invoice, :count).by(1)
  end

This is my invoice factory:

FactoryGirl.define do
    factory :invoice do
        association :client
        gross_amount 3.14
        net_amount 3.14
        number "MyString"
        payment_on "2013-01-01"
        vat_rate 0.19
    end
end

This is the create method in the invoices_controller:

def create
@client = Client.find(params[:client_id])
@invoice = @client.invoices.build(params[:invoice])

respond_to do |format|
  if @invoice.save
    format.html { redirect_to([@invoice.client, @invoice], :notice => 'Invoice was successfully created.') }
    format.json { render :json => @invoice, :status => :created, :location => [@invoice.client, @invoice] }
  else
    format.html { render :action => "new" }
    format.json { render :json => @invoice.errors, :status => :unprocessable_entity }
  end
end
end

And these are the valid attributes, ie the attributes needed for an invoice to be created successfully:

def valid_attributes
{
  gross_amount: 3.14,
  net_amount: 3.14,
  number: "MyString",
  payment_on: "2013-01-01",
  vat_rate: 0.19
}
end

These are all valid. Maybe the client_id is missing?

It is only telling me that the count did not change by one – so I am not sure what the problem is. What am I doing 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-16T21:01:53+00:00Added an answer on June 16, 2026 at 9:01 pm

    @gregates – Your answer was right, why did you remove it? 🙂 Post it again and I will check it as best answer.

    This is the solution:

    post :create, { invoice: valid_attributes, client_id: @valid_client.to_param }, valid_session
    

    instead of

    post :create, { invoice: @valid_client.invoices.build(valid_attributes), client_id: @valid_client.to_param }
    

    in the test.

    Also, I had to change the number in the valid_attributes. Debugging every single validation showed me that it was the same as in the factory – but must instead be unique. This solved it for me! Thanks for everyone’s help!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Testing out Web API for file uploading, have a simple view model like this:
Testing can be mainly classified into manual and automated testing. With regard to this
Testing in IE 8 i get this error: Object doesn't support this property or
For testing purposes, I installed .Net Framework 4 Client Profile. My tests ended and
Testing the Equals method is pretty much straight forward (as far as I know).
For testing purposes I'm trying to use SQLite, but it seems I'm missing the
Hey guys, i'm trying this example from Dave Ward blog about jQuery Templates and
Testing this jquery code on external sites (loaded via proxy to bypass Single Origin
Okay so I'm getting this weird unexpected response from Internet Explorer, while testing file
I'm trying to write a unit test that needs to confirm if a method

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.