gpt4 book ai didi

ruby-on-rails - 将数据库更改为 Postgres,现在 Rspec 抛出错误 : can't find object by id

转载 作者:太空宇宙 更新时间:2023-11-03 16:30:54 25 4
gpt4 key购买 nike

我格式化了我的电脑,在重新安装 Rails 时将我的数据库更改为 postgres,现在一些之前通过的测试失败了,例如以下代码:

describe "Tickets" do
subject { page }

describe "when creating a new ticket successfully" do
before do
login_as_user
visit new_ticket_path
fill_in "Subject", with: "Test ticket"
fill_in "Description", with: "This is a test ticket"
select 'Billing', from: "ticket[category]"
click_button "Submit Ticket"
TicketNotifier.drain
Sidekiq::Extensions::DelayedMailer.drain
end

specify { last_email.to.should include("my.email@gmail.com") }
it { should have_selector('title', text: "Ticket #1") }
it { should have_content("ticket has been submitted successfully") }
end
end

返回以下错误:

  10) Tickets when creating a new ticket successfully 
Failure/Error: TicketNotifier.drain
ActiveRecord::RecordNotFound:
Couldn't find Ticket with id=366
# ./app/workers/ticket_notifier.rb:5:in `perform'
# ./spec/requests/blas_spec.rb:14:in `block (3 levels) in <top (required)>'

# same error repeated a few times...

或者:

context "should not be able to edit other user's website" do
before { visit edit_rental_path(1) }

it { should have_selector('title', text: "Your Websites") }
it { should have_selector(notice, text: "You do not have access to this domain.") }
end

抛出以下几个:

  2) Websites create a user with 2 websites log user out log in second user should not be able to edit other user's website 
Failure/Error: before { visit edit_website_path(1) }
ActiveRecord::RecordNotFound:
Couldn't find Website with id=1
# ./app/controllers/websites_controller.rb:116:in `correct_user'
# ./spec/requests/websites_spec.rb:187:in `block (6 levels) in <top (required)>'

那么,它是否为每个 it { should ... } 子句创建一个新对象?
即使我尝试类似下面的操作,也无法通过 id 找到它:

@site = Website.where(link: "http://google.com").first
visit website_path(@site)

是什么改变导致了这些问题?

更新

我已经按照 depa 的建议清空了我的 gemset,现在我发布的第一个测试没有出现错误,即使我指定了 it { should have_selector('title', text: "Ticket #1 ")}#1 来自票证的 ID,因此出于某种原因,它正在像以前一样进行那些测试,但我仍然在其他测试上遇到错误,除非我将它们更改为:

before do
# create the ticket
@ticket = Ticket.where(...).first
end

it { should have_selector('h2', text: "Ticket ##{@ticket.id}") }

而以前,如果我只是将它保留为 Ticket #1,测试就会通过。 postgressqlite 作为测试数据库的工作方式不同吗?

最佳答案

我会尝试重置您的 gem 环境,从 gem pristine rspec-railsgem pristine capybara 开始,一直到 rvm gemset empty 如果您使用 RVM。

关于ruby-on-rails - 将数据库更改为 Postgres,现在 Rspec 抛出错误 : can't find object by id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120211/

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