- Test so the todo-list title is displayed on the page first (we use a before block, because we're going to add it in every single page):
require 'spec_helper'
describe "Viewing todo items" do
let!(:todo_list) {TodoList.create(title: "Grocery List", description: "Groceries")}
before do
visit "/todo_lists"
within "#todo_list_#{todo_list.id}" do
click_link "List Items"
end
end
it "displays the title of the todo list" do
within ("h1") do
expect(page).to have_content(todo_list.title)
end
it "displays no items when a todo list is empty"
expect(page).to have_content("TodoItems#index")
end
end
- We can pass in different formats to rspec.
- --format=documentation --> This will print the name of the test that's running as it goes through.
- We get the todo list id from the routes (rake routes).
def index
@todo_list = TodoList.find(params[:todo_list_id])
end
- The :todo_list_id goes inside of the controller (todo_items_controller.rb)
- @ symbol in front of a name makes it an instance variable: this means we'll have access to it in our view
- > index.html.erb We add that variable: <h1><%= @todo_list.title %></h1>
- Page.all cabybara method: This will find all the elements on the page with a given CSS selector.
it "displays no items when a todo list is empty"
expect(page.all("ul.todo_items li").size).to eq(0)
end
require 'spec_helper'
describe "Viewing todo items" do
let!(:todo_list) {TodoList.create(title: "Grocery List", description: "Groceries")}
def visit_todo_list(list)
visit "/todo_lists"
within "#todo_list_#{todo_list.id}" do
click_link "List Items"
end
end
it "displays the title of the todo list" do
visit_todo_list(todo_list)
within ("h1") do
expect(page).to have_content(todo_list.title)
end
end
it "displays no items when a todo list is empty"
visit_todo_list(todo_list)
expect(page.all("ul.todo_items li").size).to eq(0)
end
it "displays item content when a todo list has items" do
todo_list.todo_items.create(content: "Milk")
todo_list.todo_items.create(content: "Eggs")
visit todo_list(todo_list)
expect(page.all("ul.todo_items li").size).to eq(2)
within "ul.todo_list" do
expect(page).to have_content("Milk")
expect(page).to have_content("Eggs")
end
end
end
Alejo likes this.