Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user generator that accepts arguments (#1671) #1730

Merged
merged 11 commits into from Aug 20, 2021
4 changes: 4 additions & 0 deletions doc/default/internet.md
@@ -1,6 +1,10 @@
# Faker::Internet

```ruby
# Keyword arguments: name, username, email, password, domain_name, user_agent, uuid etc...
Faker::Internet.user #=> { username: 'alexie', email: 'alexie@example.net' }
Faker::Internet.user('username', 'email', 'password') #=> { username: 'alexie', email: 'alexie@example.net', password: 'DtEf9P8wS31iMyC' }

# Keyword arguments: name, separators
Faker::Internet.email #=> "eliza@mann.net"
Faker::Internet.email(name: 'Nancy') #=> "nancy@terry.biz"
Expand Down
7 changes: 7 additions & 0 deletions lib/faker/default/internet.rb
Expand Up @@ -277,6 +277,13 @@ def uuid
'%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString
end

def user(*args)
ashishra0 marked this conversation as resolved.
Show resolved Hide resolved
user_hash = {}
args = %w[username email] if args.empty?
args.each { |arg| user_hash[:"#{arg}"] = send(arg) }
user_hash
end

alias user_name username
end
end
Expand Down
19 changes: 19 additions & 0 deletions test/faker/default/test_faker_internet.rb
Expand Up @@ -295,4 +295,23 @@ def test_uuid
assert_equal(36, uuid.size)
assert_match(/\A\h{8}-\h{4}-4\h{3}-\h{4}-\h{12}\z/, uuid)
end

def test_user_with_args
user = @tester.user('username', 'email', 'password')
assert user[:username].match(/[a-z]+((_|\.)[a-z]+)?/)
assert user[:email].match(/.+@.+\.\w+/)
assert user[:password].match(/\w{3}/)
end

def test_user_without_args
user = @tester.user
assert user[:username].match(/[a-z]+((_|\.)[a-z]+)?/)
assert user[:email].match(/.+@.+\.\w+/)
end

def test_user_with_invalid_args
assert_raises NoMethodError do
@tester.user('xyx')
end
end
end