为用户创建多个工厂

时间:2018-05-09 06:34:42

标签: ruby-on-rails ruby-on-rails-4 rspec factory-bot rspec-rails

早些时候,我为用户提供单一工厂,并且工作正常。

但是现在,我需要两组用于用户数据的工厂,一组具有加密的详细信息,另一组具有纯文本用户详细信息。

这是我正在使用的代码:

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

使用与spec文件中相同的内容:

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

但运行规范时出现以下错误:

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

User模型没有字段“name”,而是“username”。

错误Stacktrace:

 F

故障:

1)Api :: V2 :: UserApp :: UsersController Generate Pin API为用户生成一个新的引脚      失败/错误: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)>'

完成0.37745秒(文件加载时间为4.26秒) 3个例子,1个失败

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

控制器代码

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."
    }

这是创建多个工厂的正确方法吗?为什么我得到此错误未定义的方法名称?

1 个答案:

答案 0 :(得分:0)

这不是创建变量工厂的方法。您必须对作为调用结果的值的字段使用括号:

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

看看指南: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#traits