[英]Rspec - how to write a test for class method
Being new to RoR and Rspec I am struggling to write a test for this scenario. 作为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
I want to test this method in the 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 my country_spec I have this: 在我的country_spec中,我有这个:
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
This country will be expired, a have a named scope on the model which filters out the expired countries. 该国家/地区将过期,并且在模型上有一个命名范围,用于过滤掉过期的国家/地区。 My test should be something like this:
我的测试应该是这样的:
it "should not include an expired country" do
c = Country.get_default_country_name_order
end
Is this correct so far? 到目前为止,这正确吗? The test does not seem to return anything from the method however?
测试似乎没有从该方法返回任何内容吗?
Yes, this is proper direction. 是的,这是正确的方向。
To fix problem with persisting your Country
models, you should either change this: 要解决持久化
Country
模型的问题,您应该更改以下内容:
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 your test call :country3
: 或在您的测试电话
:country3
:
it "should not include an expired country" do
country3
c = Country.get_default_country_name_order
end
let(:country3)
is only "registering" a method to be called (in your example, it populates database), but it is not performed automatically. let(:country3)
只是“注册”要调用的方法(在您的示例中,它填充数据库),但不会自动执行。 As long as you don't need the value to be returned from this method, you should stick to before
, which will execute the code automatically. 只要您不需要从此方法返回值,就应该坚持执行
before
,它将自动执行代码。
On the other hand, you might want to test returned value from your Country
model. 另一方面,您可能要测试
Country
模型返回的值。 Something like: 就像是:
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
Hope that helps. 希望能有所帮助。
Good luck! 祝好运!
UPDATE 更新
Example of how to structure the spec with multiple occurrences of before
如何构造多次出现
before
的规范的示例
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.