From 8b2780820c20cedd66ac9b914a514744c6cc0a42 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Tue, 3 Oct 2023 15:58:56 -0700 Subject: [PATCH] update for features + added flash/message assertions + adjusted urls to be http://google.com for tests for now + added @db-test tags to tests that use db interactions + added a scenario for duplicate URLs being used + added before and after steps to init and clean up db tests + added initialization of a test db for tests instead of interacting with the production database --- features/homepage.feature | 15 ++++++++++++++- features/step_definitions/steps.rb | 19 +++++++++++++++++++ features/support/env.rb | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/features/homepage.feature b/features/homepage.feature index 9215df1..4a96c4a 100644 --- a/features/homepage.feature +++ b/features/homepage.feature @@ -7,6 +7,7 @@ Feature: Homepage Scenario: Homepage Loads with a form Given I visit the "/" page Then I should see text "URL Shortener" + And I should see the message "Enter a URL" And I should see a form field "url" And I should see a "Submit" button @@ -14,9 +15,21 @@ Feature: Homepage Given I visit the "/" page When I click the "Submit" button Then I should be on "/" page + And I should see the message "Please enter a valid URL" + @db-test Scenario: Submitting the form with a correct URL Given I visit the "/" page - When I type "http://google.com/" in the "url" field + When I type "http://google.com" in the "url" field And I click the "Submit" button Then I should be on "/create" page + And I should see the message "Link created" + + @db-test + Scenario: Submitting the form with an existing URL + Given I visit the "/" page + And A link already exists with the url "http://google.com" + When I type "http://google.com" in the "url" field + And I click the "Submit" button + Then I should be on "/create" page + And I should see the message "Link exists" diff --git a/features/step_definitions/steps.rb b/features/step_definitions/steps.rb index 78eb083..9bc830e 100644 --- a/features/step_definitions/steps.rb +++ b/features/step_definitions/steps.rb @@ -1,9 +1,18 @@ +# BEFORE +Before('@db-test') do + @links = Sequel.sqlite("db/#{ENV['DB_NAME']}")[:links] +end + # GIVEN Given('I visit the {string} page') do |string| visit string end +Given('A link already exists with the url {string}') do |string| + @links.insert(url: string, code: "aaaaaa") +end + # WHEN When('I click the {string} button') do |string| @@ -31,3 +40,13 @@ end Then('I should be on {string} page') do |string| page.should have_current_path string end + +Then('I should see the message {string}') do |message| + page.should have_selector '#message', text: message +end + +# AFTER + +After('@db-test') do + @links.delete +end diff --git a/features/support/env.rb b/features/support/env.rb index 6ee27f9..c5ab798 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,11 +1,27 @@ +require_relative '../../.env' +ENV["DB_NAME"] = "test_#{ENV["DB_NAME"]}" require_relative '../../app' require 'rubygems' require 'roda' +require 'sequel' require 'capybara' require 'capybara/dsl' require 'rspec' +# DB initialization +Sequel.extension :migration +Sequel.sqlite("db/#{ENV['DB_NAME']}") do |db| + Sequel::Migrator.apply(db, "db/migrations") +end +# RSpec db setup +RSpec.configure do |c| + c.around(:each) do |example| + DB.transaction(rollback: :always, auto_savepoint: true){example.run} + end +end + +# attach app to Capybara Capybara.app = App include Capybara::DSL