简体   繁体   中英

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 to the one I've just created. 我刚刚创建的记录显示了该记录。 After this individual test, I am able to retrieve the correct record in a rails test console by issuing Ticket.last .

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?

I think the implementation of visit is not correct. If the other implementation of create_ticket is correct and you're sure about that. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM