jeudi 19 mai 2016

Rails tests for "(in)valid log-in" - explanation needed

I understand part of the code here (from Hartl's tutorial), however I'm confused about the following:

  1. I guess I still don't how testing works essentially, i.e. if say a "valid login" happens then shouldn't the "invalid login" test fail?
  2. The ordering of code lines. For instance in test "login with invalid information" if assert flash.empty? => false, then the test has alreaduy created a new sessions template (2nd line)
  3. "post login_path, session: { email: @user.email, password: "password"} line - i.e. if we give default values, how does the test work? How does it pull data for any user and matches it with the test code? . Note: added fixtures in .yml

Code here (Hartl tutorial book Ch. 8)

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  # a special setup method to create a user 
  def setup 
    # creates a user by pulling it out the user from the .yml file in fixtures
    @user = users(:sevda)
  end

  test "login with invalid information" do
    get login_path 
    assert_template 'sessions/new'
    post login_path, session: { email: "", password: ""}
    assert_not flash.empty?
    get root_path
    assert flash.empty?
  end

  test "login with valid information" do
    get login_path 
    # post the login path with the email corresponding to the user
    # we can't actually put the password -> so we'll do the 2nd best thing, that is to standarise it on the word password, i.e. all users in the databse will have the password as the string 'password'
    post login_path, session: { email: @user.email, password: "password"}
    assert_redirected_to @user
    follow_redirect!
    assert_template 'users/show'
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
  end
end

Aucun commentaire:

Enregistrer un commentaire