Home Create Multiple Factories for User
Reply: 0

Create Multiple Factories for User

user4414
1#
user4414 Published in June 19, 2018, 6:28 pm

Earlier I was having single factory for user and it worked fine.

But now, I want two sets of factories for user's data , one with encrypted details and other one with plain text user details.

This is the code I am using:

FactoryBot.define do
  factory :user do
   trait :encryption do 
    email                 AESCrypt.encrypt(Faker::Internet.email, ENV["AES_KEY"])
    password              AESCrypt.encrypt("password", ENV["AES_KEY"])
    password_confirmation AESCrypt.encrypt("password",ENV["AES_KEY"])
    username              Faker::Name.name 
    end

   trait :unencrypted_user_details do
    email                 Faker::Internet.email
    password              "password"
    password_confirmation "password"
    username              Faker::Name.name 
   end
 end
end

And using the same as in spec file:

user = FactoryBot.create(:user,:unencrypted_user_details)

But I am getting the following error while running the spec:

NoMethodError:
   undefined method `name=' for #<User:0x00000006d512f8>

The User model does not have a field "name" instead "username" is there.

Error Stacktrace:

 F

Failures:

1) Api::V2::UserApp::UsersController Generate Pin API generates a new pin for the user Failure/Error: user = FactoryBot.create(:unencrypted_user_details)

 NoMethodError:
   undefined method `name=' for #<User:0x0000000664cbb0>
   Did you mean?  name
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/activemodel-4.2.7.1/lib/active_model/attribute_methods.rb:433:in `method_missing'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:16:in `public_send'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:16:in `block (2 levels) in object'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `each'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:15:in `block in object'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `tap'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/attribute_assigner.rb:14:in `object'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/evaluation.rb:13:in `object'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/strategy/create.rb:9:in `result'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/factory.rb:43:in `run'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:29:in `block in run'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.7.1/lib/active_support/notifications.rb:166:in `instrument'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/factory_runner.rb:28:in `run'
 # /home/xxxxxxxxx/.rvm/gems/ruby-2.3.3/gems/factory_bot-4.8.2/lib/factory_bot/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
 # ./spec/controllers/user_app/users_controller.rb:43:in `block (3 levels) in <top (required)>'

Finished in 0.37745 seconds (files took 4.26 seconds to load) 3 examples, 1 failure

user_spec.rb

 describe "Generate Pin API" do
  it "generates a new pin for the user" do
    user = FactoryBot.create(:unencrypted_user_details)  #line 43

    client = user.client

    request.env["HTTP_AUTHORIZATION"] = user.auth_token

    get :generate_pin

    response_json = JSON.parse(response.body)

    expect(response_json["response_code"]).to eq(200)
  end
end

Controller Code

def generate_pin
phone = ValidatePhone.find_by(phone_no: @client.phone_no)

phone.present? ? phone.update_attributes(is_verified: true) : ValidatePhone.create(phone_no: @client.phone_no)
sms_text = "Hello"
send_sms(@client.phone_no,sms_text)
render :json => {
        :response_code => 200,
        :response_message => "Welcome Onboard."
    }

end

Is this the correct way to create multiple factories and why am I getting this error undefined method name?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.320382 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO