Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Chris McKnight
Chris McKnight
11,045 Points

Todo List Application Destroy Spec failing

I am getting the following failure for the deleting todo lists section. I downloaded the project files and the spec failed there as well.

Failures:

  1) Destroying todo lists is successful when clicking destroy link
     Failure/Error: expect(TodoList.count).to eq(0)

       expected: 0
            got: 1

       (compared using ==)
     # ./spec/features/todo_lists/destroy_spec.rb:15:in `block (2 levels) in <top (required)>'

Finished in 1.02 seconds
41 examples, 1 failure, 2 pending

I believe the problem is due to rails using unobtrusive javascript for destroy actions. I installed the poltergeist gem as a javascript web driver, set js: true on the describe block of the destroy spec and added the following two lines to the spec helper.

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

Finally, I added the following code to spec/support/shared_db_connection.rb from Ryan Bates' railscast so the database connection is shared.

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

7 Answers

Joachim McClain
Joachim McClain
1,278 Points

Hey Chris,

I solved this issue by running TodoList.destroy_all For some reason I had an existing todo_list in the test. So this was first three lines of code...

require 'spec_helper'

describe "Creating todo lists" do TodoList.destroy_all

Ran rspec spec/features/todo_lists/destroy_spec.rb

Everything passed, removed the line TodoList.destroy_all and everything still passed :)

Cheers

Joa

John Salzarulo
John Salzarulo
6,596 Points

I had this exact issue as described above. Joa's solution worked perfectly for me. Thanks!

Chris McKnight
Chris McKnight
11,045 Points

Joa, this is actually a different problem. In my case, the clicking the destroy button doesn't actually work when using capybara without JavaScript. Rails uses unobtrusive JavaScript when using the link_to helper. I got around it using the above capybara JavaScript driver, poltergeist. I believe button_to will degrade gracefully but I have not tried.

Also, there is a better way to accomplish clearing out the test database. There is a gem called database cleaner which will clear out all of models at the times you specify in your spec helper.

Naomi Freeman
STAFF
Naomi Freeman
Treehouse Guest Teacher

When I was following along, it was simply a matter of an inaccurate number of "end"s. Can we see your code?

Chris McKnight
Chris McKnight
11,045 Points
require 'spec_helper'

describe 'Destroying todo lists', js: true do

  let!(:todo_list) { TodoList.create(title: 'Groceries', description: 'Grocery list') }

  it 'is successful when clicking destroy link' do
    visit '/todo_lists'

    within "#todo_list_#{todo_list.id}" do
      click_link 'Destroy'
    end

    expect(page).to_not have_content(todo_list.title)
    expect(TodoList.count).to eq(0)
  end

end
Naomi Freeman
STAFF
Naomi Freeman
Treehouse Guest Teacher

Sorry I didn't see this! The comment never notified me :( I didn't get notified til the answer.

Chris McKnight
Chris McKnight
11,045 Points

No problem, Naomi. Strange that it didn't notify you.

Joachim McClain
Joachim McClain
1,278 Points

Thanks Chris,

I'll definitely look into the gem and thanks for the clarification on the issue.

Cheers

Joa

Chris McKnight
Chris McKnight
11,045 Points

No problem, Joa. Glad to help.