Skip to content

Adding specs (mostly refactoring of helper-methods) #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 6, 2023
184 changes: 184 additions & 0 deletions spec/features/1_basic_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
require "rails_helper"

describe "User authentication" do
it "requires sign in before any action with the Devise `before_action :authenticate_user!` method", points: 2 do
visit "/movies/new"
current_url = page.current_path

expect(current_url).to eq(new_user_session_path),
Copy link
Contributor

@jelaniwoods jelaniwoods Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When Devise isn't installed this will fail since new_user_session_path isn't defined. That won't make the custom error message display though, since the expectation never ran.
image

If your intention was that the custom error message would be visible to students who haven't created users, then you should use the String path instead. Something like:

    expect(current_url).to eq("/users/sign_in"),

I also buy the idea that since the spec title already mentions Devise, student could infer that they need devise to make the test pass w/out the help of the error message. It's up to you if you want to make this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also buy the idea that since the spec title already mentions Devise, student could infer that they need devise to make the test pass w/out the help of the error message. It's up to you if you want to make this change.

Yeah I considered my use of the helper method there, but that's why I include Devise in the spec. The assumption is that they should have added devise and installed it, which will provide those helper methods.

Merging! Thanks for all your help!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember now that students tend to work top down from the list of specs, so placing the Devise-required specs at the bottom will probably eliminate some friction. It's not a big deal though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sure. Just going to make that change directly on main.

"Expected `before_action :authenticate_user!` in `ApplicationController` to redirect guest to /users/sign_in before visiting another page."
end

it "allows a user to sign up", points: 2 do
old_users_count = User.count
visit new_user_registration_path

fill_in "Email", with: "user@example.com"
fill_in "Password", with: "password"
fill_in "Password confirmation", with: "password"
click_button "Sign up"

new_users_count = User.count

expect(old_users_count).to be < new_users_count,
"Expected 'Sign up' form on /users/sign_up to successfully add a User record to the database."
end
end

describe "The /movies page" do
before do
sign_in_user if user_model_exists?
end

it "can be visited", points: 1 do
visit "/movies"

expect(page.status_code).to be(200),
"Expected to visit /movies successfully."
end

it "has a link to add a movie", points: 1 do
visit "/movies"

expect(page).to have_link('Add a new movie', href: "/movies/new"),
"Expected /movies to have an 'Add a new movie' link to '/movies/new'."
end

it "has a large, light-themed bootstrap navbar", points: 1 do
visit "/movies"

expect(page).to have_selector("nav[class='navbar navbar-expand-lg navbar-light bg-light']"),
"Expected /movies to have have a <nav class='navbar navbar-expand-lg navbar-light bg-light'> bootstrap navbar."
end

it "has margin top spacing with a bootstrap container class", points: 1 do
visit "/movies"

expect(page).to have_selector("div[class='container mt-3']"),
"Expected /movies to have have a <div class='container mt-3'> bootstrap container for adding margin top spacing."
end
end

describe "The /movies/new page" do
before do
sign_in_user if user_model_exists?
end

it "can be visited", points: 1 do
visit "/movies/new"

expect(page.status_code).to be(200),
"Expected to visit /movies/new successfully."
end

it "has a form", points: 1 do
visit "/movies/new"

expect(page).to have_selector("form[action='/movies']"),
"Expected /movies/new to have a form with action='/movies'."
end

it "has a form that creates a movie record", point: 1 do
old_movies_count = Movie.count
visit "/movies/new"

fill_in "Title", with: "My test movie"
fill_in "Description", with: "description"
click_button "Create Movie"

new_movies_count = Movie.count

expect(old_movies_count).to be < new_movies_count,
"Expected 'Create Movie' form on /movies/new to successfully add a Movie record to the database."
end

it "displays a success notice flash message after creating movie", point: 1 do
visit "/movies/new"

fill_in "Title", with: "My test movie"
fill_in "Description", with: "description"
click_button "Create Movie"

expect(page).to have_content("Movie created successfully."),
"Expected to see the notice flash message 'Movie created successfully' after filling in and submitting the /movies/new form."
end
end

describe "The movie details page" do
let(:movie) { Movie.create(title: "My title", description: "My description") }

before do
sign_in_user if user_model_exists?
end

it "can be visited", points: 1 do
visit "/movies/#{movie.id}"

expect(page.status_code).to be(200),
"Expected to visit /movies/ID successfully."
end

it "shows the movie on a bootstrap card", points: 2 do
visit "/movies/#{movie.id}"

expect(page).to have_selector("div[class='card']"),
"Expected /movies/ID to have have a <div class='card'> element to display the movie."
end

it "has a Font Awesome trash can icon to delete the movie", points: 2 do
visit "/movies/#{movie.id}"

expect(page).to have_selector("i[class='fa-regular fa-trash-can']"),
"Expected /movies/ID to have a Font Awesome trash can icon on the card, with class='fa-regular fa-trash-can'."
end

it "deletes the movie with a DELETE request", points: 2 do
visit "/movies/#{movie.id}"

expect(page).to have_selector("a[href='/movies/#{movie.id}'][data-method='delete']"),
"Expected /movies/ID to have 'Delete Movie' link with the proper data-method='delete'."
end
end

describe "The movie edit page" do
let(:movie) { Movie.create(title: "My title", description: "My description") }

before do
sign_in_user if user_model_exists?
end

it "can be visited", points: 1 do
visit "/movies/#{movie.id}/edit"

expect(page.status_code).to be(200),
"Expected to visit /movies/ID/edit successfully."
end

it "has a form", points: 1 do
visit "/movies/#{movie.id}/edit"

expect(page).to have_selector("form[action='/movies/#{movie.id}'][method='post']"),
"Expected /movies/ID/edit to have a form with action='/movies/ID' and method='post'."
end

it "has a hidden patch input", points: 2 do
visit "/movies/#{movie.id}/edit"

expect(page).to have_selector("input[name='_method'][value='patch']", visible: false),
"Expected the edit movie form to have an input field of type='hidden' with name='_method' and value='patch'."
end
end


def sign_in_user
user = User.create(email: "alice@example.com", password: "password")
visit new_user_session_path

fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Log in"
end

def user_model_exists?
Object.const_defined?("User")
end
7 changes: 0 additions & 7 deletions spec/features/dummy_spec.rb

This file was deleted.