mind.blank
mind.blank

Reputation: 4880

Changed database to Postgres, now Rspec throwing errors: can't find object by id

I've formatted my computer and while reinstalling Rails changed my database to postgres, and now a few tests that were previously passing are failing, for example the following code:

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("[email protected]") }
    it { should have_selector('title', text: "Ticket #1") }
    it { should have_content("ticket has been submitted successfully") }
  end
end

Returns the following errors:

  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...

Or:

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

Throwing a few of the following:

  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)>'

So, is it creating a new object for every it { should ... } clause?
Even when I try something like the following it can't find by id:

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

What has changed that's creating these issues?

Update

I've emptied my gemset as suggested by depa and now I don't get errors for the first test I posted, even when I specify it { should have_selector('title', text: "Ticket #1") }. The #1 comes from the ID of the ticket, so for some reason it's working on those tests like it used to, but I'm still getting errors on others unless I change them to:

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

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

Whereas previously if I just left it as Ticket #1 the tests would pass. Do postgres and sqlite work differently as test databases?

Upvotes: 2

Views: 421

Answers (1)

Marcelo De Polli
Marcelo De Polli

Reputation: 29291

I would try resetting your gem environment, starting from gem pristine rspec-rails and gem pristine capybara and going as far as rvm gemset empty if you use RVM.

Upvotes: 1

Related Questions