Search code examples
rspecfactory-botrspec-rails

How to create a fake data for testing my ruby on rails controller


I want to the test my rails controllers with rails-rspec gem. I already set up everything for testing(ran all the commands for rspec, install the rspec-gaurd and etc), however I wasn't sure what to use to create a fake data to pass it for my controllers. I chose factory-girl-rails for that?(if this is wrong gem or there is something better that I need to know please let me know). my controller params should look like this:

{
  "name": "example",
  "job" : "TheJob"
  "config": "{THIS IS A HASH}"
}

and my factory looks like this: enter image description here

and This is what my rspec test looks like: enter image description here

Is my test format right? because I am getting an error

Trait not registered: config_info

When Guard ran the code!


Solution

  • Factory girl can be used to create the data, depending upon your needs it might be overkill. but you should be able to do something like this..

    FactoryGirl.define do
      factory :user do
        name "myName"
        last "myLast"
        config { { family: "MyFamily", parent: "MyParent" } } 
      end
    end
    

    note the use of 2 sets of { } since you will need to pass the hash inside the block assignment form of the factory.

    or to match the params that you requested (As opposed to the factory you posted).

    FactoryGirl.define do
          factory :user do
            name "myName"
            job "TheJob"
            config { { family: "MyFamily", parent: "MyParent" } } 
          end
        end