0
RoR和Rspec是新手我很努力爲這種情況編寫測試。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
在我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
這個國家會過期,一個有一個名爲範圍在過濾掉過期國家的模型上。我的測試應該是這樣的:
it "should not include an expired country" do
c = Country.get_default_country_name_order
end
這到目前爲止是否正確?該測試似乎沒有從該方法返回任何內容?
多次出現結構規範例子中,我更新了我的OP,以顯示如何描述和之前做。這是否會影響你的答案? – user3437721
嘿@ user3437721,包含的代碼並沒有真正改變太多,你可能會在你的規範中出現多次'before'。請查看更新後的答案,瞭解如何構建您的規格示例 –
偉大的幫助,讓它工作正常!謝謝 – user3437721