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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:03:17+00:00 2026-06-16T23:03:17+00:00

I am new to rails and have just started writing tests using rspec version

  • 0

I am new to rails and have just started writing tests using rspec version : 2.11.1 . I am looking for a way to seed different data for different tests in my class. For this I created a static function in the test itself. Depending on the requirements I am in instantiating different number of objects. I have a function seed_data which instantiates different number of objects based on the number passed to it. I get this exception :

NoMethodError: undefined method `seed_data' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5::Nested_1:0x007fcedd9bea70>

Please have a look at the sample code below :

require 'spec_helper'
require 'go_live_sale'
require 'rspec/expectations'

describe GoLiveSale do


  context "get_go_live_sale_for_sale_ids Function Correctness" do

    describe "Testing get_go_live_sale_for_sale_ids() function correctness " do
      it "should return error when sale_ids is blank" do
         result = GoLiveSale.get_go_live_sale_for_sale_ids({})
         result[:err].should_not be_blank
         result[:err].should  == "err1"
      end

      it "should return all columns corresponding to select field for single go_live_sale" do
        go_live_sales = seed_data(1)
        sale_ids =  go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
        result = GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids})
        result[:err].should be_blank
        result[:go_live_sales].count.should_be == 1
        delete_seed_data(go_live_sales)
      end

      it "should return all columns corresponding to select field for multiple go_live_sale" do
        go_live_sales = seed_data(2)
        sale_ids =  go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
        GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids})
        result[:err].should be_blank
        result[:message].should be_blank
        result[:go_live_sales].count.should == 2
        delete_seed_data(go_live_sales)
      end

      it "should return selected columns corresponding to select field for single go_live_sale" do
        go_live_sales = seed_data(1)
        sale_ids =  go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
        result = GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids, :columns => ["id","sale_id"]})
        result[:err].should be_blank
        result[:go_live_sales].count.should_be == 1
        delete_seed_data(go_live_sales)
      end

      it "should return selected columns corresponding to select field for multiple go_live_sale" do
        go_live_sales = seed_data(2)
        sale_ids =  go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
        GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids,  :columns => ["id","sale_id"]})
        result[:err].should be_blank
        result[:message].should be_blank
        result[:go_live_sales].count.should == 2
        delete_seed_data(go_live_sales)
      end

      it "should return error when selecting erroneous columns" do
        go_live_sales = seed_data(2)
        sale_ids =  go_live_sales.map{ |go_live_sale| go_live_sale.sale_id}
        GoLiveSale.get_go_live_sale_for_sale_ids({:sale_ids => sale_ids,  :columns => ["id","random_sale_id"]})
        result[:err].should_not be_blank
        delete_seed_data(go_live_sales)
      end

    end
  end
end

def self.delete_seed_data(go_live_sales)
  go_live_sales.each do |go_live_sale|
    go_live_sale.delete
  end
end

def self.seed_data(number_of_go_live_sale_to_create)
  go_live_sales =[]
  (1..number_of_go_live_sale_to_create).each do |number|
    go_live_sales.push(create_go_live_sale(number))
  end
  return go_live_sales
end

def self.create_go_live_sale(number_to_add)
  go_live_sale  =  GoLiveSale.new
  go_live_sale.start_date = Time.now
  go_live_sale.sale_id = Sale.select("IFNULL(max(id),0)+#{number_to_add} as sale_id").first.try(:sale_id)
  go_live_sale.sale_name = "Test Sale" + go_live_sale.sale_id.to_s
  go_live_sale.sale_type = "Flash Sale"+ go_live_sale.sale_id.to_s
  User.current_user = User.first
  go_live_sale.save
  return go_live_sale
end

RSpec::Matchers.define :be_valid do
  match_for_should do |actual|
    actual[:err].blank?
    actual[:validation_error].blank?
    actual[:is_valid] == true
  end

  match_for_should_not do |actual|
    actual[:err].present?
    actual[:validation_error].present?
    actual[:is_valid] == false
  end

  failure_message_for_should do |actual|
    "Expected validation to pass, but it failed"
  end

  failure_message_for_should_not do |actual|
    "Expected validation to fail, but it passed"
  end
end

I understand that it is some scope issue or maybe rspec doesn’t let you write the tests that way. It will be great if some one can write a small snippet of code explaining how to instantiate test data in such cases. My rails version is 3.0.5.

  • 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-16T23:03:18+00:00Added an answer on June 16, 2026 at 11:03 pm

    You can just get rid of the self. in the method definition and move it inside the describe GoLiveSale do block, then call it with seed_data as expected.

    For example:

    describe GoLiveSale do
      def my_method
      end
      context "some context" do
        it "should call my_method" do
          expect {my_method}.not_to raise_error
        end
      end
    end
    

    This spec should pass.

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

Sidebar

Related Questions

I have just started with ruby on rails, have made a new rails project
I've just started to write an application using the latest version of Rails and
I have just started coding with rails. I am using: Rails 3.2.8 ruby 1.9.3p194
I am new Rspec and just started by generating a new controller on Rails
I have just started using Rails and I am trying to build a banking
I just started looking into Ruby on Rails and have been reading Dave Thomas's
I am new to ruby on rails and I have just started watching rails
I am new to ruby on rails and I have just started watching rails
I just started using rails, I have a model generated with a set of
I have started developing a new Rails app on my server using RVM, Rails

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.