ActiveRecord::QueryMethods 在 rspec 测试中不起作用,但在控制台中工作正常
我有一个测试,我想测试book_group与书籍关联时无法删除,该测试失败了,但是在应用程序中,此功能正常,
describe 'callbacks' do
context 'before_validation' do
include_examples 'examples for strippable attributes', :book_group, :name, :description, :spawn_button_label
end
it 'is not destroyed and returns a base error if there is an book associated to the it' do
error = 'Cannot delete record because dependent books exist'
book_group.books << create(:book)
expect(book_group.destroy).to be false
end
end
我在测试中调试了,发现错误是因为此查询无法用作预计
- 首先,我有效,这两个模型具有关联
pry(#&lt; rspec ::示例groups :: bookgroup :: callbacks&gt;)&gt; book_group.books =&gt; [#&lt; book:0x0000563cb3f6f6ef0 ID:1, 评论_id:1, 名称:“ Vernie Becker”, 等级:“站点”, book_group_id:831812, fure_book_id:nil]
- 我进行下一个查询,但结果是错误的
book_group.books.s.where(fure_book_id:nil).order(id :: desc).first =&gt; nil
,但是控制台内的查询按预期工作,
[4] pry(main)> @book_group.books.where(author_book_id: nil).order(id: :desc).first DEBUG
[2022-04-02T16:16:49.295Z] book Load (0.7ms) SELECT `books`.* FROM `books` WHERE `books`.`book_group_id` = 6 AND `books`.`author_book_id` IS NULL ORDER BY `books`.`id` DESC LIMIT 1
=> #<book:0x000055d2217339c8 id: 261, review_id: 1, name: "Base book site", level: "site", book_group_id: 6, book_template_id: 2, author_book_id: nil]
以这种方式创建了
def create
ActiveRecord::Base.transaction do
@book_group = bookGroup.new(book_group_params)
@book_group.author_id = params[:author_id]
@book_group.save!
AllowedActorship.create_from_level_scoped_params(
book_group_params,
@book_group
)
render(
json: { message: @book_group },
status: :ok
)
end
end
我已经重置并准备了BD,我不确定为什么它如此奇怪,我会说谢谢您对此有所帮助。
I have a test where I want to test that a book_group cannot delete when is associated with a book, this test failed but in the application, this feature works fine
describe 'callbacks' do
context 'before_validation' do
include_examples 'examples for strippable attributes', :book_group, :name, :description, :spawn_button_label
end
it 'is not destroyed and returns a base error if there is an book associated to the it' do
error = 'Cannot delete record because dependent books exist'
book_group.books << create(:book)
expect(book_group.destroy).to be false
end
end
I debugged into the test and found that the error is because this query not working as expected
- First, I valid that these two models have an association
pry(#<RSpec::ExampleGroups::bookGroup::Callbacks>)> book_group.books => [#<book:0x0000563cb3f6eaf0 id: 1, review_id: 1, name: "Vernie Becker", level: "site", book_group_id: 831812, author_book_id: nil]
- I do the next query but its result is wrong
book_group.books.where(author_book_id: nil).order(id: :desc).first
=> nil
but this query within console working as expected
[4] pry(main)> @book_group.books.where(author_book_id: nil).order(id: :desc).first DEBUG
[2022-04-02T16:16:49.295Z] book Load (0.7ms) SELECT `books`.* FROM `books` WHERE `books`.`book_group_id` = 6 AND `books`.`author_book_id` IS NULL ORDER BY `books`.`id` DESC LIMIT 1
=> #<book:0x000055d2217339c8 id: 261, review_id: 1, name: "Base book site", level: "site", book_group_id: 6, book_template_id: 2, author_book_id: nil]
the book_group is created in this way
def create
ActiveRecord::Base.transaction do
@book_group = bookGroup.new(book_group_params)
@book_group.author_id = params[:author_id]
@book_group.save!
AllowedActorship.create_from_level_scoped_params(
book_group_params,
@book_group
)
render(
json: { message: @book_group },
status: :ok
)
end
end
I already have reset and prepared the bd, I'm not sure why it working so weird, I will say thank you for whatever helped with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论