gpt4 book ai didi

ruby-on-rails - rails : Undefined method 'name' in 'user' factory OR Undefined Method for all factories

转载 作者:行者123 更新时间:2023-11-28 20:35:07 28 4
gpt4 key购买 nike

我目前正在使用 RSpec 和 Factory_Bot_Rails gem 来测试应用程序,但我遇到了以下问题。

当使用 factory_bot_rails 版本 5.0.2 gem 时,我的工厂现在都给我所有注册工厂的未定义方法错误。如果我将 gem 的版本降级到 4.11,它就可以工作。

例如,对于我的用户工厂,它有 name 作为定义的属性之一,它显示错误

“用户”工厂中未定义方法“名称”

我已经很努力地解决了这个问题,但这是不可行的。

我们将不胜感激任何形式的帮助。

最佳答案

这是我解决问题的方法

从 Thoughtbot 的博客中,我开始意识到静态定义的属性已被弃用,然后从 factory_bot >= 5.0 中删除。因此,不要为您的工厂使用静态定义的属性,而是使用动态定义的属性。也就是说,在工厂中的每个已定义属性周围插入大括号{}

举个例子

使用动态定义的属性

factory :robot do
name { "Ralph" }
email { "ralph@example.com" }
end

而不是静态定义的属性

factory :robot do
name "Ralph"
email "ralph@example.com"
end

你可以在这里阅读更多相关信息

Deprecating static attributes in factory_bot 4.11

关于ruby-on-rails - rails : Undefined method 'name' in 'user' factory OR Undefined Method for all factories,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57126906/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com