Using FactoryGirl without Rails, ActiveRecord or any database with RSpec

I was wondering if anyone knows if FactoryGirl can be used without any of the above preconditions.

I would like to use it to generate test data on the fly when testing user interface automation for both mobile and web sites, and even for the API.

I know that I could create some custom helper classes / methods and use getters and setters, etc., but I thought it would be nice to use this amazing little stone.

I searched quite extensively and also tried to create a basic RSpec project (I also tried Cucumber), but to no avail. It still seems that I need classes to be created with the appropriate login to consume it.

FactoryGirl.define do factory :user do firstname { Faker::Name.first_name } lastname { Faker::Name.last_name } age { 1 + rand(100) } end end 

Then, if I try to call it in the RSpec file ...

 describe... it... user = build_stubbed(:user) end end 

I also read the docs and tried all the other options, I just keep getting ...

  Failure/Error: user = build_stubbed(:user) NameError: uninitialized constant User 

which assumes that I need a User class with logic.

+6
source share
1 answer

Your guess is correct - you still have to write the User class yourself. factory_girl works great with non-ActiveRecord classes, but doesn't write them for you.

By default, factory_girl creates instances by calling new without parameters, so your class must either not def initialize or initialize not accept parameters, like the ActiveRecord model. Therefore, it is easiest to write User like this:

 class User attr_accessor :firstname, :lastname, :age end 

If you decide that you need initialize , which requires parameters, you can use factory_girl initialize_with to tell him how to instantiate the class .

+5
source

Source: https://habr.com/ru/post/971147/


All Articles