[英]Model testing with MiniTest?
我正在使用MiniTest框架,並想編寫一個模型測試。 這是我的測試代碼:
it "must find or create authentication" do
auth = Authentication.find_by_provider_and_uid( @auth.provider,
@auth.uid )
val = auth.nil?
if val==true
Authentication.create_with_omniauth @auth
end
end
此測試檢查以查看Authentication.find_by_provider_and_uid
方法是否存在,如果auth
為nil,它將創建一個新的auth
。
我使用if
子句編寫它,但是我不知道它是否正確。 我該如何糾正該測試?
因為您的問題中沒有代碼,所以我假設您正在使用minitest-rails並對其進行了正確配置,因為這是我最熟悉的。
假設您具有以下代碼:
class Authentication < ActiveRecord::Base
def self.find_by_provider_and_uid provider, uid
self.where(provider: provider, uid: uid).first_or_initalize
end
end
此外,我假設您在test/fixtures/authentications.yml
具有以下燈具數據
test_auth:
provider: twitter
uid: abc123
user: test_user
我將進行類似於以下的測試:
describe Authentication do
describe "find_by_provider_and_uid" do
it "retrieves existing authentication records" do
existing_auth = authentications :test_auth
found_auth = Authentication.find_by_provider_and_uid existing_auth.provider, existing_auth.uid
refute_nil found_auth, "It should return an object"
assert found_auth.persisted?, "The record should have existed previously"
assert_equal existing_auth, found_auth
end
it "creates a new authentication of one doesn't exist" do
new_auth = Authentication.find_by_provider_and_uid "twitter", "IDONTEXIST"
refute_nil new_auth, "It should return an object"
assert new_auth.new_record?, "The record should not have existed previously"
end
end
end
FWIW,我不喜歡此方法的名稱。 名稱與動態查找器相似,但是行為不同。 我將方法重命名為for_provider_and_uid
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.