Skip to content
This repository has been archived by the owner on Mar 30, 2018. It is now read-only.

Latest commit

 

History

History
114 lines (90 loc) · 2.42 KB

05_xunit_patterns.md

File metadata and controls

114 lines (90 loc) · 2.42 KB

Obscure Tests

It is difficult to understand the test at a glance.

Mystery Guest

What is a mystery guest?

it "downcases the location on save" do
  expect(user.location).to eq "default"
end

Note: The first thing you may find asking yourself is "Where does the user variable come from?"

before do
  user = create(:user, location: 'default')
end

Mystery Variables

What is a mystery variable?

it "downcases the location on save" do
  user = create(:user)

  expect(user.location).to eq "default"
end

Note: The first thing you may find asking yourself is "Why is the location set to 'default'"

FactoryBot.define do
  factory :user do
    location 'default'
  end
end

lets(:not)

Why you should consider the cases for when you should and shouldn't use let statements

let!(:person) {
  create :person,
  gender: female,
  date_of_birth: date_of_birth,
  city_of_birth: city,
  state_of_birth: state,
  country_of_birth: country,
  county_of_birth: county,
  number_of_dependents: dependents,
  nickname: nickname,
  materials_under_another_name: materials,
  alt_first_name: alt_first,
  alt_middle_name: alt_middle,
  alt_last_name: alt_last,
}

let(:person_presenter) {
  PersonPresenter.new(person)
}

describe '#person_presenter' do
  specify { person_presenter('birth_location').must_include city.name }
end

Note: In this example, each of the variables inside the let is another let that makes one or more calls to the database. Notice that this is a let!, so it will execute before each spec, adding 7 seconds for each test even if the other tests don't reference the data.

describe '#person_presenter' do
  it "shows the birth location as the city" do
    city_name = "Raleigh"
    city = build_stubbed(:city, name: city_name)
    person = build_stubbed(:person, city_of_birth: city)

    presenter = PersonPresenter.new(person)

    presenter("birth_location").must_include city_name
  end
end

Great! Now we've written tests and saw a few things we should watch for

What do you want to do next?

  • Write more unit specs?
  • Write more request specs?
  • Try it out in our main app?