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 Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Creating Methods in Tests

Why include 'expect(page).to_not have_content("This is what I'm doing today.")' after the refactor?

I don't immediately see the benefit of the last two lines in the following test:

    it "displays an error when the todo list has no title" do
      create_todo_list(title: "")
      expect(page).to have_content("error")
      expect(TodoList.count).to eq(0)

      visit "/todo_lists"
      expect(page).to_not have_content("This is what I'm doing today.")
    end

Given our new method, the last line will always return true. We already check that the app returns an error and TodoList.count == 0 ... isn't that enough for the test?

2 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

So testing is all a matter of personal preference. Here, we are doing feature testing (which is like integration testing, testing everything working together.) We are not just testing the controller, or the model, or the view, but how everything works together.

I agree that the last line is kind of redundant and could be removed since there isn't a case where it wouldn't save to the database and then would show up on the todo_list index page.

Vinny Harris-Riviello
Vinny Harris-Riviello
11,898 Points

It is just testing that the listing was not created. It is possibly redundant like Brandon mentions, but it is a valid way to verifying the list did not get created. We already checked that the count is 0, but it double checks or to the least. It shows you, you have several options to run the test. So it is worth knowing them for your personal toolbox. ;)