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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:21:00+00:00 2026-06-06T18:21:00+00:00

I am trying to write a test case for the depot application. Below is

  • 0

I am trying to write a test case for the depot application. Below is the code

Model:

class Product < ActiveRecord::Base
 attr_accessible :description, :image_url, :price, :title

 validates :title, :description, :image_url, :presence => true
 validates :price, :numericality => {:greater_than_or_equal_to => 0.01}

end

Test Case:

require 'spec_helper'

describe Product do
before :each do
    @product = Product.new "Title", "Description", "Image Url", "Price"
end

describe "#new" do
    it "takes four parameters and returns a product" do
        @product.should be_an_instance_of Product
    end
end

describe "#title" do
    it "returns the correct title" do
        @product.title.should eql "Title"
    end
end

describe "#description" do
    it "returns the correct description" do
        @product.description.should eql "Description"
    end
end

describe "#image_url" do
    it "returns the correct image_url" do
        @product.image_url.should eql "Image Url"
    end
end

describe "#price" do
    it "returns the correct price" do
        @product.price.should eql "Price"
    end
end

end

I am getting the errors

undefined method `has_key?' for nil:NilClass 

and

ArgumentError:←[0m
1mwrong number of arguments (4 for 2)←[0m
 ./spec/product_spec.rb:5:in `new'←[0m
 ./spec/product_spec.rb:5:in `block (2 levels) \
 in <top (required)>'" for all examples. 

Please guide me on how to correct it

  • 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-06T18:21:01+00:00Added an answer on June 6, 2026 at 6:21 pm

    Try something like this:

    before :each do
       @product = Product.new(title: "Title", description: "Description", image_url: "Image Url", price: "Price")
    end
    

    Try Michael Hartl’s awesome tutorial

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

Sidebar

Related Questions

I am trying to write an Android test case on a block of code
I am trying to write code to test a method by mocking the class
This is for Play! Framework 2.0. I'm trying to write a simple test case
I'm trying to write a test for ActiveRecord - and Rails uses MiniTest for
I'm trying to write a unit test for a piece of code that generates
I'm trying to write a test case for Selenium and I'm having a little
I am trying to write a simple integration test on a Rails 3.1 application
I'm trying to write some test cases for my Qooxdoo application, and I think
I am trying to write a test case using xUnit.net and the AutoRollback attribute
I am trying to come up with a test case for a Queue class

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.