2013-03-05 5 views
1

Я использую среду 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 равен нулю, то будет создан новый 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.

Смежные вопросы