User signup page - Actualize-Apprenticeship-Team-troy/personal-dashboard GitHub Wiki

See glossary at the bottom of this wiki

Generate a User Model

=> rails g model user username email password_digest

=> rails db:migrate

Generate the User Controller

=> rails g controller users new create

Write tests for user model

Add the following to the user spec file

# user_spec.rb
require 'rails_helper'

RSpec.describe User, type: :model do
  describe "User validations" do
    describe "validates the presence of expected attributes" do
      it "positive result" do
        expect(User.new(username: 'username', email: '[email protected]', password: 'password'))
          .to be_valid
      end

      it "negative result, missing attributes" do 
        expect(User.new(username: 'username')).to_not be_valid
      end
    end

    describe "validates that username is unique, and more then 3 characters" do
      it "negative result, username to short" do 
        expect(User.new(username: 'us', email: '[email protected]', password: 'password'))
          .to_not be_valid
      end

      it "negative result, username is not unique" do 
        user1 = User.new(username: 'username', email: '[email protected]', password: 'password')
        user1.save
        user2 = User.new(username: 'username', email: '[email protected]', password: 'password')
        result = user2.save
        expect(result).to be(false)
      end
    end

    describe "validates that emails is valid and unique" do
      it "negative result email is not valid" do
        expect(User.new(username: 'username', email: 'test.com', password: 'password'))
          .to_not be_valid
      end

      it "negative result email is not unique" do
        user1 = User.new(username: 'username', email: '[email protected]', password: 'password')
        user1.save
        user2 = User.new(username: 'username2', email: '[email protected]', password: 'password')
        result = user2.save
        expect(result).to be(false)
        expect(user2.errors[:email]).to eq(['has already been taken'])
      end
    end

  end

  describe "validates that password is of valid length - 6 characters" do
    it "negative result, password to short" do 
      expect(User.new(username: 'username', email: '[email protected]', password: 'pass'))
        .to_not be_valid
    end
  end
end

create a factory for the user

# spec/factory/users.rb
FactoryBot.define do
  factory User do
    name { Faker::Name }
    email '[email protected]'
    password 'password'
  end
end

=> rspec

Should have 10 examples, 7 failures, and 2 pending

Now fix the failing tests

Validation resources

Write tests for the Users controller

require 'rails_helper'

RSpec.describe UsersController, type: :controller do

  describe "GET #new" do
    it "returns http success" do
      get :new
      expect(response).to have_http_status(:success)
    end
  end

  describe "POST #create" do
    describe "create a new user" do
      it "positive result, user is created" do
        params = { user: {
          username: 'username',
          email: '[email protected]',
          password: 'password'
        } }

        get :create, params: params
        users = User.all
        expect(users.length).to eq(1)
        expect(subject.request.flash[:success]).to eq('Successfully signed up')
        expect(response).to redirect_to( root_path )
      end

      it "negative result, password not long enough" do
        params = { user: {
          username: 'username',
          email: '[email protected]',
          password: 'pass'
        } }

        get :create, params: params
        users = User.all
        expect(users.length).to eq(0)
        expect(subject.request.flash[:warning]).to eq('Form is invalid')
        expect(subject).to render_template(:new)

      end
    end
  end
end

Should have 13 examples, 3 failures, and 2 pending

Now fix the failing tests

Glossary

=> run command in terminal