질문에 코드가 없으므로 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
으로 바꿉니다.