Я пытаюсь использовать вспомогательный метод на всех моих фабриках, но я не могу заставить его работать. Здесь моя настройка:
Модуль помощника (в spec/support/test_helpers.rb)
module Tests
module Helpers
# not guaranteed to be unique, useful for generating passwords
def random_string(length = 20)
chars = ['A'..'Z', 'a'..'z', '0'..'9'].map{|r|r.to_a}.flatten
(0...length).map{ chars[rand(chars.size)] }.join
end
end
end
A factory (в spec/factories/users.rb)
FactoryGirl.define do
factory :user do
sequence(:username) { |n| "username-#{n}" }
password random_string
password_confirmation { |u| u.password }
end
end
Если я запускаю свои тесты (с помощью rake spec
), я получаю следующую ошибку везде, где я создаю пользователя с Factory(:user)
:
Failure/Error: Factory(:user)
ArgumentError:
Not registered: random_string
Что мне нужно сделать, чтобы использовать random_string
на моих фабриках?
Я пробовал следующее:
- используя
include Tests::Helpers
на каждом уровне моего factory (доdefine
, междуdefine
иfactory :user
и внутриfactory :user
) - в
spec_helper.rb
, у меня уже есть следующее:config.include Tests::Helpers
и он дает мне доступ кrandom_string
в моих спецификациях - просто требуется файл в моем factory
Я также прочел следующие ссылки без успеха: