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