简体   繁体   English

如何检索和使用在RSpec / Capybara测试中创建的记录的ID?

[英]How can I retrieve and use the ID of a record I've created inside an RSpec/Capybara test?

I'm trying to visit a page for an object I'm creating inside an individual test. 我正在尝试访问在单个测试中创建的对象的页面。

it "should display some text" do
  log_employee_in
  create_ticket
  visit "/tickets/#{Ticket.last.id}"
  page.should have_content 'Some Text'
end

Inside the scope of this test, Ticket.last brings up the record prior to the one I've just created. 在此测试的范围内,Ticket.last 我刚刚创建的记录之前显示了该记录。 After this individual test, I am able to retrieve the correct record in a rails test console by issuing Ticket.last . 完成此单个测试之后,我可以通过发出Ticket.last在rails测试控制台中检索正确的记录。

It seems like the record isn't truly being persisted to the database until after the current test concludes. 在当前测试结束之前,似乎记录并没有真正保存到数据库中。

How can I retrieve this ID inside the test? 如何在测试中检索此ID?

I think the implementation of visit is not correct. 我认为实施访问是不正确的。 If the other implementation of create_ticket is correct and you're sure about that. 如果create_ticket的其他实现是正确的,那么您可以确定。 You can try this: 您可以尝试以下方法:

it "should display some text" do
  log_employee_in
  create_ticket
  visit ticket_path(Ticket.last.id) # you should be even able to get the ticket with Ticket.last
  page.should have_content 'Some Text'
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此查询不使用我创建的索引? - Why doesn't this query use the indexes i've created? 我创建了 oneToMany 关系,但我如何获得单条记录属于 Sequalize 中的多条记录 - I created oneToMany relation but how I can get the single record belongs to the many record in Sequalize 如何使用变量的 ID 创建记录? - How can I create a record with an ID from a variable? 您好,如何根据 ID 在一列中显示 2 条记录? - Hello, how can I show 2 record in one column based on ID? 在使用LIMIT后如何对结果进行排序? - How can I ORDER the result after i've used LIMIT? 如何从我使用 C 设置的 SQL 数据库中提取特定记录,然后将该记录的一部分存储在变量中? - How do I pull a specific record from an SQL database I've set up using C, and then store part of that record in a variable? 我如何附加一个新的 id 来记录 - How do I append a new id to record 如何覆盖从 Odoo 中的另一个 model 创建的记录的 create 方法? - How can I override the create method of a record created from another model in Odoo? 我可以将行记录用作表的标题吗? - Can I use the row record as header for a table? 我可以将数据库记录标记为“正在使用”吗? - Can I mark a database record as “in use”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM