ruby on rails - Rspec - how to write a test for class method -


being new ror , rspec struggling write test scenario.

# table name: countries # #  id             :integer          not null, primary key #  code           :string(255)      not null #  name           :string(255) #  display_order  :integer #  create_user_id :integer          not null #  update_user_id :integer #  created_at     :datetime         not null #  updated_at     :datetime #  eff_date       :date #  exp_date       :date 

i want test method in country model:

 def self.get_default_country_name_order         countries = country.in_effect.all.where("id !=?" ,wbconstants::default_country_id).order("name")     result = countries   end 

in country_spec have this:

describe country   before(:all)     databasecleaner.clean_with(:truncation)   end   let(:user){create(:user)}   let(:country3){create(:country,code:"aus", name:"australia", create_user:user, eff_date: time.new(9999,12,31), exp_date: time.new(9999,12,31))}    after(:all)     databasecleaner.clean_with(:truncation)   end 

this country expired, have named scope on model filters out expired countries. test should this:

it "should not include expired country"    c = country.get_default_country_name_order   end 

is correct far? test not seem return method however?

yes, proper direction.

to fix problem persisting country models, should either change this:

let(:country3){create(:country,code:"aus", name:"australia", create_user:user, eff_date: time.new(9999,12,31), exp_date: time.new(9999,12,31))} 

to this:

before {create(:country,code:"aus", name:"australia", create_user:user, eff_date: time.new(9999,12,31), exp_date: time.new(9999,12,31))} 

or in test call :country3:

it "should not include expired country"   country3   c = country.get_default_country_name_order end 

let(:country3) "registering" method called (in example, populates database), not performed automatically. long don't need value returned method, should stick before, execute code automatically.

on other hand, might want test returned value country model. like:

it "should not include expired country"   example_country = country3   c = country.get_default_country_name_order   expect(c).to eq example_country end 

hope helps.

good luck!

update

example of how structure spec multiple occurrences of before

describe country   before(:all)     databasecleaner.clean_with(:truncation)   end   let(:user){create(:user)}   let(:country3){create(:country,code:"aus", name:"australia", create_user:user, eff_date: time.new(9999,12,31), exp_date: time.new(9999,12,31))}    after(:all)     databasecleaner.clean_with(:truncation)   end    describe "#get_default_country_name_order"     # can register "before"     before {create(:country,code:"aus", name:"australia", create_user:user, eff_date: time.new(9999,12,31), exp_date: time.new(9999,12,31))}      # or simpler - call method      # before "it", , create record     # before { country3 }      "should not include expired country"       # expectations here     end   end 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -