Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rspec-如何为类方法编写测试_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails Rspec-如何为类方法编写测试

Ruby on rails Rspec-如何为类方法编写测试,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,作为RoR和Rspec的新手,我正在努力为这个场景编写一个测试 # Table name: countries # # id :integer not null, primary key # code :string(255) not null # name :string(255) # display_order :integer # create_user_id :integer

作为RoR和Rspec的新手,我正在努力为这个场景编写一个测试

# 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
我想在国家/地区模型中测试此方法:

 def self.get_default_country_name_order
        countries = Country.in_effect.all.where("id !=?" ,WBConstants::DEFAULT_COUNTRY_ID).order("name")
    result = countries
  end
在我的国家,我有:

describe Country do
  before(:all) do
    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) do
    DatabaseCleaner.clean_with(:truncation)
  end
此国家/地区将过期,在模型上有一个命名范围,用于过滤过期国家/地区。我的测试应该是这样的:

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

到目前为止这是正确的吗?然而,测试似乎没有从方法中返回任何东西

是的,这是正确的方向

要解决保留您的国家/地区模型的问题,您应该更改以下内容:

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))}
为此:

before {create(:country,code:"AUS", name:"Australia", create_user:user, eff_date: Time.new(9999,12,31), exp_date: Time.new(9999,12,31))}
或者在您的测试电话中:country3:

let:country3只注册了一个要在您的示例中调用的方法,它填充数据库,但不会自动执行。只要不需要从这个方法返回值,就应该坚持使用before,它将自动执行代码

另一方面,您可能希望测试从您的国家/地区模型返回的值。比如:

it "should not include an expired country" do
  example_country = country3
  c = Country.get_default_country_name_order
  expect(c).to eq example_country
end
希望有帮助

祝你好运

更新

有关如何使用多次出现的before来构造等级库的示例


我已经更新了我的OP,以显示如何描述和之前做。这会影响你的答案吗?嘿@user3437721,包含的代码实际上没有多大变化,你的规范中可能不止一次出现过这种情况。请查看更新的答案,以了解如何构建规范的示例
it "should not include an expired country" do
  example_country = country3
  c = Country.get_default_country_name_order
  expect(c).to eq example_country
end
describe Country do
  before(:all) do
    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) do
    DatabaseCleaner.clean_with(:truncation)
  end

  describe "#get_default_country_name_order" do
    # you can register another "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 - this will call your method 
    # before "it", and create the record
    # before { country3 }

    it "should not include an expired country" do
      # your expectations here
    end
  end