Testing for Job Board
The snippet can be accessed without any authentication.
Authored by
Brigitte Jellinek
Use these tests step by step, as you are building the app.
jobs_test.rb 4.34 KiB
# ====================================================================
# integration tests run against rails with HTTP requests
# and can analyse the returned html. javascript is not included.
# integration tests are fast.
# ====================================================================
# this is a first, very basic version, of test/integration/job_flow_test.rb
require 'test_helper'
class JobFlowTest < ActionDispatch::IntegrationTest
test "visiting the index" do
get jobs_url
assert_response :success
assert_select "h1", text: "RailsBridgeCorp Open Jobs"
end
test "visiting the new job form" do
get new_job_url
assert_response :success
assert_select "h1", text: "Add a job"
end
test "can create a new job" do
assert_difference "Job.count", 1 do
post jobs_url,
params: { job: { title: "security expert", description: "use apis without forms." } }
assert_response :redirect
end
end
test "visiting the index shows all the jobs" do
j = jobs(:one) # created by fixture - don't forget to set up a fixture file (see below)
get jobs_url
assert_response :success
assert_select "h3", text: j.title
end
test "nav bar is present" do
get jobs_url
assert_response :success
assert_select "a", text: "Add Job"
assert_select "a", text: "Home"
end
test "visiting the edit job form" do
j = jobs(:one)
get edit_job_url(j.id)
assert_response :success
assert_select "h1", text: "Edit Posting"
assert_select "label", text: "Description"
assert_select "input", value: j.description
end
test "can edit an existing job" do
j = jobs(:one)
new_title = "dark hat hacker"
patch "/jobs/#{j.id}",
params: { job: { title: new_title, description: j.description } }
assert_response :redirect
j = Job.find(j.id)
assert_equal new_title, j.title
end
test "can delete job" do
j = jobs(:one)
assert_difference "Job.count", -1 do
delete "/jobs/#{j.id}"
assert_response :redirect
end
end
end
# ====================================================================
# this is file test/models/job_test.rb
require 'test_helper'
class JobTest < ActiveSupport::TestCase
test "can create and save a job" do
j = Job.new
assert j.save
end
end
# ====================================================================
# this is file test/fixtures/jobs.yml
one:
title: Frontend Developer
description: makes pretty things. handles ie6.
two:
title: Backend Developer
description: makes invisible things. no ie6.
# ====================================================================
# system tests re used to test the browser / javascript also.
# ====================================================================
# this is a first, very basic version, of test/system/jobs_test.rb
require "application_system_test_case"
class JobsTest < ApplicationSystemTestCase
test "visiting the index" do
visit jobs_url
assert_selector "h1", text: "RailsBridgeCorp Open Jobs"
end
end
# ====================================================================
# these is a more elaborate version of test/system/jobs_test.rb
# don't copy all of it at once!
require "application_system_test_case"
class JobsTest < ApplicationSystemTestCase
setup do
@job = jobs(:one)
end
test "visiting the index" do
visit jobs_url
assert_selector "h1", text: "RailsBridgeCorp Open Jobs"
end
test "creating a Job" do
visit jobs_url
click_on "New Job"
fill_in "Description", with: @job.description
fill_in "Title", with: @job.title
click_on "Create Job"
assert_text "Job was successfully created"
click_on "Back"
end
test "updating a Job" do
visit jobs_url
click_on "Edit", match: :first
fill_in "Description", with: @job.description
fill_in "Title", with: @job.title
click_on "Update Job"
assert_text "Job was successfully updated"
click_on "Back"
end
test "destroying a Job" do
visit jobs_url
page.accept_confirm do
click_on "Destroy", match: :first
end
assert_text "Job was successfully destroyed"
end
end
# this is file test/fixtures/jobs.yml
one:
title: Frontend Developer
description: do pretty things, handle ie6
two:
title: Backend Developer
description: do invisibile things, no ie6
Please register or sign in to comment