- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个网站,用户可以在其中创建产品,并且产品可以有 list 。
我有一个包含以下列的 list 表:-
id | product_id | content | archived
我是 Rails 测试的新手。我在下面写了测试
test 'should have content' do
checklist = Checklist.new
assert checklist.save
end
然后用下面的命令运行测试
ruby -I test test/models/checklist_test.rb
测试失败,出现Expected false to be truthy 错误。
是否因为可以使用 user.product.checklists 访问 list 的问题,我必须先在固定装置中填充数据并在测试中调用它们?
编辑 1
我在 list 模型中没有任何验证。
class Checklist < ApplicationRecord
belongs_to :product
end
我在测试中添加了 ! 保存,如下所示
test 'should have content' do
checklist = Checklist.new
assert checklist.save!
end
得到这个错误
ActiveRecord::RecordInvalid: Validation failed: Product must exist
因为表中有 product_id。我不知道如何向 Rails 测试提供数据。有帮助吗?
编辑 2
如下修改后错误消除。
class Checklist < ApplicationRecord
belongs_to :product, optional: true
end
但是我想用 product
测试模型展示。我不知道如何使用固定装置向测试提供数据,就好像没有我可以使用的外键一样 Checklist.new
在测试中。
因为它有外键,我如何向 Checklist
提供数据?因为它属于 Product
它本身属于 User
?
最佳答案
checklist.save
如果 checklist
由于某种原因未能保存,将返回 false
;大概是因为验证失败。
例如,您的 app/models/checklists.rb
可能包含如下内容:
validates :product_id, presence: true
或者:
validates :content, length: { minimum: 10 }
等等。在这个简单的场景中,您可能只需查看模型定义就可以轻松确定错误;但对于更复杂的应用程序,您可以查看:checklist.errors.messages
以查看记录未能保存的原因列表。
从测试名称(“应该有内容”)来看,我的猜测是它失败了,因为content
不能为空!
例如,为了让这个测试通过,你可能需要这样写:
test 'should have content' do
checklist = Checklist.new(content: 'hello world')
assert checklist.save
end
人们在测试这类事情时使用的一种常见方法是在 factories 中定义一个“有效记录” ;这使您可以显式测试无效 记录,而不必在很多地方显式地重新定义有效记录。例如,在这里你可以这样做:
# test/factories/checklists.rb
FactoryBot.define do
factory :checklist do
content 'test content'
end
end
# test/models/checklist_test.rb
test 'should have content' do
checklist = build(:checklist, content: nil)
refute checklist.save # Expecting this to FAIL!
assert_includes "Content cannot be nil", checklist.errors
end
(代码可能不是 100% 完整/准确;但您明白了)
关于ruby-on-rails - Rails 测试模型说 'Expected false to be truthy',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49862493/
class test { public static void main(String[] args){ Object o1 = new Object(); O
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
这个问题在这里已经有了答案: Why does the expression 0 >> (True == False) is False True >>> True == (False is Fals
为什么在 Python 中它是这样评估的: >>> False is False is False True 但是当用括号尝试时表现如预期: >>> (False is False) is False
我有一个名为“apple”的表,我编写了以下查询: select name, count(name), case when istasty is null then fal
python boolean 逻辑中的运算符优先级 print(False==True or False) #answer is True print(False==(False or True))#
请不要看条件,因为它们在这里是为了便于理解行为 为什么 result 等于 true ? boolean result = false && (false)?false:true; 我知道我们可以通过
乍一看,这篇文章可能看起来像是重复的,但事实并非如此。相信我,我已经查看了所有 Stack Overflow,但都无济于事。 无论如何,我从 Html.CheckBoxFor 得到了一些奇怪的行为。
这个问题在这里已经有了答案: python operator precedence of in and comparison (4 个答案) 关闭 6 年前。 我的一位前辈演示了它,我想知道这是否是
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
为什么 {} == false 评估为 false 而 [] == false 评估为 true在 javascript 中? 最佳答案 这是根据 Abstract Equality Comparis
这个问题在这里已经有了答案: Why does (1 in [1,0] == True) evaluate to False? (1 个回答) 关闭7年前。 为什么使用括号时这些语句按预期工作: >>
我试过搜索这个,但我真的不知道如何表达它以查看是否有其他人发布了答案。 但是,我正在制作一个国际象棋游戏和一个人工智能来配合它,这是非常困难的,我的问题是当我检查两个棋子是否在同一个团队时我必须做 (
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
为什么 为 false || null 返回与 null || 不同的结果错误? 我可以安全地依赖 return myVar || false 如果 myVar 为 null 或 false,则返回
我正在尝试遵循 NHibernate 教程,“你的第一个基于 NHibernate 的应用程序:修订 #4”在 NHibernate Forge。 但线路:new SchemaExport(cfg).
这个问题在这里已经有了答案: Empty list boolean value (3 个答案) 关闭 4 年前。 我是 Python 的新手,不理解以下行为: 为什么要声明 [] == False
以下函数循环访问对象的值。如果值为空this.hasInvalidValue设置为true ,如果不为空 this.hasInvalidValue设置为false : user: { email:
所以我正在玩 java.lang.reflect 东西并尝试制作类似 this 的东西。这是我的问题(可能是一个错误): 将字段设置为 true 的方法的代码: private static void
当我在编程时,我的 if 语句出现了意想不到的结果。 这个代码警报怎么会是真的?我在 W3S 没有找到任何可以帮助我的东西,我真的很想知道为什么这些警报是“正确的” window.alert(fals
我是一名优秀的程序员,十分优秀!