2013-03-05 2 views
1

MiniTest 프레임 워크를 사용 중이고 모델 테스트를 작성하려고합니다. 이것은 내 테스트 코드는 다음 Authentication.find_by_provider_and_uid 방법이 존재하는 경우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 

이 테스트 여부를 확인하고, auth이 전무 경우, 그것은 새로운 auth을 만듭니다.

if 절을 사용하여 작성했지만 사실인지 아닌지는 잘 모르겠습니다. 이 테스트를 어떻게 수정합니까?

답변

1

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