Topic: How to access the methods in the module and test them

Hi

I need to test a module.

My unit test is as follows:

class JobTest < Test::Unit::TestCase
    def test_should_require_user
     assert_required_fields :user
  end

  def test_job_soft_delete
    job = Job.new
    job.soft_delete
   
    assert_equal -1, job(:graphic_design).status_id
    assert job(:graphic_design).deleted?
  end

My module is as follows:

class Job < ActiveRecord::Base
def soft_delete
    update_attribute(:status_id, -1)
  end


How to write the unit test for above module. How to call the methods in the module from the test class. In the above case I get an error. It says i cannot create object for the class Job inside the class Jobtest.

Please help me.


Thanks
Janakiraman

Re: How to access the methods in the module and test them

Modules can't be instantiated, but you can include the module in the test:

class JobTest < Test::Unit::TestCase
  include Job
  #...
end

Then you can call the "soft_delete" method on the test itself and it will call this method.

However, this method calls update_attribute, does it expect to be included in a model? If so, test the model and not just the module directly.

Railscasts - Free Ruby on Rails Screencasts

Re: How to access the methods in the module and test them

Hi

I have written the following test script. When I execute it it say wrong number of arguments passed( 1 of 0). can you help me.


Test script

require 'test/unit'

class JobTest < Test::Unit::TestCase
  # fixtures :settings, :job_drafts, :jobs, :users, :zips, :categories, :medias, :attachable_medias
 
  #def setup
    #set_settings
    #setup_medias
  #end
 
  #def teardown
    #teardown_medias
  #end

   def test_job_soft_delete
    job = jobs(:graphic_design)
    job.soft_delete
   
    assert_equal -1, jobs(:graphic_design).status_id
    assert jobs(:graphic_design).deleted?
  end


private

  def jobs
      { :graphic_design => 0}
    end
end


The corresponding module is
def soft_delete
    update_attribute(:status_id, -1)
  end


Is this way of writing the test script is right. If not how well can i implement this test script. please explain.


Thanks
Janakiraman

Re: How to access the methods in the module and test them

I'm confused, is Job a model? How does the module fit in here and what is the name of the module? Why are you creating a module instead putting it directly into the model?

Railscasts - Free Ruby on Rails Screencasts

Re: How to access the methods in the module and test them

Hi

The name of the module is "Job. I need to write a test script to test the method in the module. How to do this.

The method in the module si
class Job < ActiveRecord::Base

def soft_delete
    update_attribute(:status_id, -1)
  end

Re: How to access the methods in the module and test them

From looking at your code, Job isn't a module, it's a model, so I'm assuming that's what you meant.

Going back to your problem, can you post the full error message you are getting along with the stack trace?

Railscasts - Free Ruby on Rails Screencasts

Re: How to access the methods in the module and test them

Hi

Sorry for the late response.
This is my test file under unit directory.

class JobTest < Test::Unit::TestCase

  fixtures :jobs

 

def setup

    set_settings

    setup_medias

end

 

def teardown

    teardown_medias

end




def test_for_job_active
    job = jobs(:spanish_translator)
    job.active?   
 
    assert_equal 1,jobs(:spanish_translator).status_id
end
end


This is method under model

def active?
    status_id == 1
  end

When i run this test I get error like

ActiveRecord::StatementInvalid: RuntimeError: ERROR     C42703  Mcolumn "view_phone" of relation "jobs" does not exist  P172    Fparse_target.c L767    RcheckInsertTargets: INSERT INTO jobs ("updated_at", "view_email", "priceraw", "title", "type_id", "id", "description", "category_id", "phone1", "zip_id", "posted_at", "status_id", "user_id", "view_phone", "pricing_id", "location", "created_at") VALUES ('Mon May 14 18:49:57 +0530 2007', 't', 12500, 'Your Sensetive Floral Artist', 0, 5, 'Why, sensetive?  I am a psychology student and I enjoy creating floral designs that invoke the mood that you would like to experience, such as festive, peaceful... flowers have meanings, example, the red rose is for love...  And color invokes sensation.', 1, '(123) 098-7654', 5, 'Mon May 14 18:49:57 +0530 2007', 1, 1, 't', 1, '(45.49894,-122.68781),(45.49894,-122.68781)', 'Mon May 14 18:49:57 +0530 2007')
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract_adapter.rb:128:in `log'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/postgresql_adapter.rb:152:in `execute'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:290:in `insert_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:289:in `each'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:289:in `insert_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:257:in `create_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:257:in `each'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:257:in `create_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:59:in `transaction'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:255:in `create_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:867:in `silence'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:248:in `create_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:593:in `load_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:538:in `setup_with_fixtures'
    /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:575:in `setup'



and whe i comment out the line #fixtures: jobs

I get undefined method jobs.


Where Am i wrong. please help me out in testing this small test case. Iam helpless and dont know how to resolve this issue.


Thanks
Janakiraman

Re: How to access the methods in the module and test them

Looks like the jobs table doesn't have a "view_phone" column but you're trying to set one in the fixtures. Check your table to make sure the column exists.

Railscasts - Free Ruby on Rails Screencasts

Re: How to access the methods in the module and test them

Hi

Thanks. I just want to confirm the way i wrote the test script is right and meaningful. Iam not sure of the way to write it. If the way i have written the script is wrong, I just want you guide me and point me the best way to write the script.


Thanks
Janakiraman