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

推荐订阅源

S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
Jina AI
Jina AI
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
V
V2EX
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
B
Blog
博客园 - 叶小钗
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security 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