How do I loop if Faker generates an existing name for the name field?
The name field is the primary key in this table. When it finds a duplicate name it will generate until it finds a different name.
class Crud
include HTTParty
base_uri 'http://dummy.restapiexample.com/api/v1'
def create
nome = Faker::Name.first_name
salario = Faker::Number.decimal(l_digits: 4, r_digits: 2)
idade = Faker::Number.number(digits: 2)
#note, you should pass body as JSON string
body = { name: nome, salary: salario, age: idade }.to_json
headers = {
'Accept' => 'application/vnd.tasksmanager.v2',
'Content-Type' => 'application/json'
}
self.class.post('/create', body: body, headers: headers)
end
def retrieve(id)
self.class.get("/employee/#{ id }")
end
end
> client = Crud.new
> response = client.create
> id = JSON.parse(response)['id']
> client.retrieve(id)
Faker::Name.unique.first_name
should always return a unique name per test run. The caveat is that if you generate a large number it might run out of unique values and raise an error.
You also must clear your database between test runs, or it could generate collisions.