惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
月光博客
月光博客
爱范儿
爱范儿
D
Docker
U
Unit 42
P
Proofpoint News Feed
I
InfoQ
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
LangChain Blog
V
Visual Studio Blog
IT之家
IT之家
Vercel News
Vercel News
G
Google Developers Blog
M
MIT News - Artificial intelligence
美团技术团队
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog

Codebase Audits & Rescue | Ally Piechowski

Most of your tech debt is free Your Setup Script Should Support Git Worktrees How to Be a Good Open Source Maintainer "The Git Commands I Run Before Reading Any Code" "Ruby 3.2 Is EOL: What You Actually Need to Do" "Rails 7.2 to 8.1 Upgrade: What Actually Breaks and How to Fix It" "Why Your Engineering Team Is Slow (It's the Codebase, Not the People)" "Migrating from Sprockets to Propshaft: Is It Worth It?" "How to Close a Tab in Vim" "How I Audit a Legacy Rails Codebase in the First Week" "How to Open a New Tab in Vim" "Rails default_scope: Why You Should Never Use It" "Solved: ActionController::ParameterMissing (param is missing or the value is empty)" "Solved: Warning: Using the last argument as keyword parameters is deprecated" "Vim: How to Open Current Opened File in New Tab" "Rails: How to Use Greater Than/Less Than in Active Record where Statements" "What is the best time for stand-up meetings?" "What Is Fed vs Unfed Sourdough Starter?" "My Father, My Mentor, My Teacher" "How to Get Over Burnout" "What's the difference between a Good Developer and a Good Googler?" Codebase Audits & Rescue | Ally Piechowski
"Using Let and Context to Modularize RSpec Tests"
"Ally Piechowski" · 2019-12-24 · via Codebase Audits & Rescue | Ally Piechowski

When test suites contain a lot of duplication, coupling occurs at an individual spec basis. By utilizing core RSpec functionality, developers can clean up specs in order to reduce duplication and add clarity to the focal point of the spec.

Ally Piechowski · · 2 min read
Using Let and Context to Modularize RSpec Tests

When test suites contain a lot of duplication, coupling occurs at an individual spec basis. By utilizing core RSpec functionality, developers can clean up specs in order to reduce duplication and add clarity to the focal point of the spec.

If I want to test an invalid and valid email, it’s pretty easy to test with a basic RSpec test:

RSpec.describe User do
  it '[email protected] is a valid email' do
    expect(
        User.new(
          name: 'John Doe',
          email: '[email protected]',
          phone: '555-555-5555'
        )
    ).to be_valid
  end
  
  it 'johnatexampledotcom is not a valid email' do
    expect(
      User.new(
        name: 'John Doe',
        email: 'johnatexampledotcom',
        phone: '555-555-5555'
      )
  ).to_not be_valid
  end
end

While this may work for the first few specs, this starts to get a bit tedious and duplicative.

This is not optimal because our setup code has a lot of duplication and this duplicated code.

The RSpec Let + Context Pattern

By using core Rspec functionality, we can spruce up these 2 tests like so:

RSpec.describe User do
  subject do
    User.new(
      name: 'John Doe',
      email: email,
      phone: '555-555-5555'
    )
  end
  let(:email) { '[email protected]' }

  context 'when email is [email protected]' do
    let(:email) { '[email protected]' }

    it do
      expect(subject).to be_valid
    end
  end

  context 'when email is testfakeemail (does not contain @)' do
    let(:email) { 'testfakeemail' }

    it do
      expect(subject).to_not be_valid
    end
  end
end

This provides us with:

  • DRY specs
  • Re-usable setup code
    • Additional specs within each context
    • New specs, such as “phone number validations”
  • Easily interpretable differences between specs

The context / let pattern brings the most clarity into the differences between the 2 tests. This pattern can be used with in combination with Factories in order to make my tests extra clear and extra dry. Additionally, this pattern typically results in a more thoughtful test output.


Related Articles