[英]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.