Skip to content

Commit

Permalink
Add user generator that accepts arguments (faker-ruby#1671)
Browse files Browse the repository at this point in the history
  The new user class method will now allow you to do this

  ```
  user = Faker::Internet.user('name', 'email', password')
  => {'name' => 'John Doe', 'email' => 'fake_email@test.com', 'password' => 'pass123' }
  ```
  • Loading branch information
ashishra0 committed Sep 6, 2019
1 parent 515fdbf commit 32468c2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
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)
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

0 comments on commit 32468c2

Please sign in to comment.