DEV Community

Cover image for Custom Rake File for UI testing: CSV's and Faker
Anna Villarreal
Anna Villarreal

Posted on

Custom Rake File for UI testing: CSV's and Faker

So I needed a custom rake file for my project for testing my UI. I had never done this on my own before, so I'm going to share my experience as a beginner, so the other new guys can all understand too!

Rake file, what is that?

You can run a rake file to perform a specific task. I need Users, events, and followers. So lets do that with rake.

It all started out with me creating actual events on my website. I soon realized that this was a tedious thing to be doing. I had made 14 or so events and I didn't want to loose all my hard work. So I downloaded a csv from the database to save it. First time doing that. So of course I have to figure out how to use that in my rake file as well.


First we have to set up our files in our project.

project files


We make a csv folder for the csvs and we put our rake file in the tasks folder.

The first thing I did was create a task for my csv file in my rake file.
a rake file name should be something like 'awesome_data.rake'. Mind the file extensions. In the awesome_date.rake file we just made, we set it up by picking a name and a description:



setup


The namespace is what we will call on the left side of the semicolon as you will see later. The task will be on the right. We set up the task below as below.



task

I have chosen the task name events. Under the require 'csv', we set a variable equal to the file we want rails to read from. We also need to tell rails to parse the file. This is important so it knows what to put where. My csv file includes headers(column names) so I set that to true.



Image description

This next part is where your custom needs come into play. For each row on my csv file, I am telling rails I need an Event created. My Event class matches up to my csv file perfectly, since the file was made from my database. All I have to do is set the params of my Event class equal to its corresponding spot on the csv file. Sounds worse than it is. Here is my complete task:



events task

We are looping through each row. We grab the data then hop to the next row. It is very simple to use a csv! I admit, I was a bit terrified and it sounded like a daunting task. But as with everything, with small steps, nothing is impossible.

As you can see, I am setting the id to the id on the csv. At first I didn't think rails would like it since it automatically creates id's for each instance in rake. Turns out, you can explicitly set it. This is especially useful if you want your id's to match up because they are linked to something important out there in the cosmos, like say, your photos in the cloud. XD

This way, I can have my 14 solid events every time. Photos matching and everything.

Run the rake file by typing rake csv:events in your terminal. Remember this is the name you picked.


Takeaway

Now, if you have no data at all, you can use the faker gem to make instances of whatever. Here is another snippet of code from todays adventure:



fake people

You can create a bunch of users for testing! You can assign all of their relevant fake info (hahaha) and make them do things, like have followers and stuff.

I can tell you, I was laughing to myself earlier when I couldn't make fake people follow each other. That was pretty epic. What's even more epic is I figured it out.



fake people following each other

Thanks for checking out my adventure.

Install faker gem! Faker Gem

Top comments (0)