Поскольку в вашем вопросе нет кода, я собираюсь предположить, что вы используете 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
.