Vishal
Vishal

Reputation: 7361

How to send fake JSON data in a Rails response

I know we can generate fake data with faker in Ruby.

I want to send fake data from the API in the response. Currently, I generated a dummy response in the public/data.json file which has only three static objects.

def dummy_response
 data = File.read("#{Rails.root}/public/data.json")
 render :json => data, status: 200
end

I have to send 100 dummy objects with random values. Can I use the yml file to send dummy data or with faker gem generate dummy JSON and send it back in response?

Upvotes: 1

Views: 1043

Answers (2)

NM Pennypacker
NM Pennypacker

Reputation: 6942

You could do something like this:

data_hash = {}
100.times { |x| data_hash[x] = { name: Faker::Name.first_name } }
render json: data_hash.to_json, status: 200

Upvotes: 1

Felix
Felix

Reputation: 4716

Yes, Chuck Norris can use the Faker gem.

def dummy_response
 data = {fact: Faker::ChuckNorris.fact}
 render :json => data, status: 200
end

Upvotes: 0

Related Questions