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 #1729

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/faker/default/internet.rb
Expand Up @@ -277,6 +277,14 @@ def uuid
'%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString
end

def user(*args)
user_hash = {}
args.each do |arg|
user_hash[:"#{arg}"] = send(arg)
end
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('name', 'email', 'password')
assert user['name'].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['name'].match(/[a-z]+((_|\.)[a-z]+)?/)
assert user['email'].match(/.+@.+\.\w+/)
end

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