为什么对不在Rails RSPEC模型规范中设置的主题上的此算法?
我有一个规范测试一个如下所示的模型:
RSpec.describe SomeModel, type: :model do
subject { described_class.new(test_amount: 99) }
describe 'validates a column' do
it 'does some validations' do
expect(subject).to validate_presence_of(:test_amount)
end
end
end
一个如下所示的模型:
class SomeModel < ApplicationRecord
validates :test_amount, presence: true
end
在架构中,它的列看起来像这样,带有一个非空集:
t.integer "test_amount", default: 0, null: false
无论我做什么或将代码放在哪里, test_amount 都是测试和错误时始终为零。 我尝试过移动测试线,将主题放在前面等等,但总是 数据库抛出非空错误,即使我在模型代码中提出 test_amount 值不是 99,而是 nil。如果我提高测试值 a before 像这样:
before do
raise subject.test_amount
end
这确实会导致 99,但是如果我删除它,它总是为零,并且当它到达测试的预期部分时会抛出错误。
为了让这个测试正常工作,我缺少什么,在实际测试步骤中进行测试时,我似乎无法将 test_amount
设置为 99。
测试总是抛出错误:
PG::NotNullViolation: ERROR: null value in column "test_amount" of relation "some_models"违反了非空约束
或类似的,但在验证之前要检查test_mount
的值,但未设置。
感谢您的帮助,我觉得我在这里缺少一些非常基本的东西。
I have a spec testing a model that looks like this:
RSpec.describe SomeModel, type: :model do
subject { described_class.new(test_amount: 99) }
describe 'validates a column' do
it 'does some validations' do
expect(subject).to validate_presence_of(:test_amount)
end
end
end
And a model that looks like this:
class SomeModel < ApplicationRecord
validates :test_amount, presence: true
end
And in the schema it's column looks like this with a not-null set:
t.integer "test_amount", default: 0, null: false
No matter what I do or where I put the code, test_amount is always nil when being tests and errors.
I've tried moving the test lines around, putting the subject in a before etc, but always the
database is throwing a non-null error and even if I raise in the model code
the test_amount value is not 99 it is nil. If I raise the test value in
a before like this:
before do
raise subject.test_amount
end
That does result in a 99, however if I remove this, it is always nil and throws an error when it gets to the expect part of the test.
What am I missing in order to get this test to work, I just cannot seem to get the test_amount
to set to 99 when being tested in the actual test step.
The test always throws the error:
PG::NotNullViolation: ERROR: null value in column "test_amount" of relation "some_models" violates not-null constraint
or similar, I have but in before-validations to check the value of test_amount
and it does not get set.
Thanks for you help, I feel like there's something really basic I'm missing here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,验证 -
此外,尝试将此行移动到
describe
块内 -First of all, verify that -
Moreover, try moving this line inside the
describe
block -这有可能是由于未在测试环境中运行迁移吗?
除此之外,我认为,因为我们不是保存记录数据库。我们不会期望验证会进行。
按此文档 或
记录#保存!
已被调用。运行
记录#新
时,我们正在创建一个新实例,但不保存到我们的数据库中。When running
Record#create
我们初始化记录,然后通过调用record#save
将其保存到数据库中。Any chance this is due to not running migrations in the test environment?
Other than this I would think that because we aren't saving the record to the database. We wouldn't expect validations to be ran.
As per this documentation we are only expecting to run validations when
Record#save
orRecord#save!
has been called.When running
Record#new
we are creating a new instance but not saving to our database.When running
Record#create
we initialize the record and then save this to the database by callingRecord#save
.您可以用有效吗? /a> and errors> errors> errors 类似下面的方法:
You can write test cases for the
presence
validation with valid? and errors methods like below: