Logo

dev-resources.site

for different kinds of informations.

On testing

Published at
9/25/2023
Categories
rspec
testing
tests
Author
epigene
Categories
3 categories in total
rspec
open
testing
open
tests
open
Author
7 person written this
epigene
open
On testing

Why test?

  • Improves development quality and speed - needed state is usually faster and easier to set up and manipulate in test environment.
  • Establishes confidence that things work as needed and will continue to do so.
  • Serves as documentation.
  • Drives better design - if it's hard to test, it's poorly designed.

What to test?

Follow J.B. Rainsberger's idea of "isolated tests" - test pieces of source code in a single file, usually public methods.

Do contract testing - stub out calls to other class methods, but assert that the calls happen with correct parameters and that code under test correctly handles the mocked return value. This way code close to input/output (for example controllers) need not run through the whole app stack.

# sample action
def update
  record = find_user!
  return render_record_missing_error unless record

  result = SomeService.call(
    user: record,
    company: current_company,
    options: safe_params.to_hash,
  )

  if result.success?
    render(json: result, status: :ok)   
  else
    render(json: result, status: :unprocessable_entity)
  end 
end
Enter fullscreen mode Exit fullscreen mode

Focusing on only testing the code written in the file worked on tells us that SomeService.call should be treated as a black box and stubbed out. We do not care what side-effects it produces, only that code under test can use its return value properly.
Furthermore, we have three ways out of this method:

  1. early return due to missing user and two proper results
  2. a success
  3. a failure

Covering these scenarios should reach 100% coverage without excessive specs.

# sample good spec
describe SomeController, "update" do
  context "when user can not be found" do
    it { responds_with_record_missing }
  end

  context "when SomeService call returns a success outcome" do
    before do
      allow(SomeService).to receive(:call).and_return(success_outcome)
    end

    it do
      responds_with_success_json

      expect(SomeService).to have_received(:call).with(
        user: user,
        company: company,
        options: {some: param},
      ).once
    end
  end

  context "when SomeService call returns a failure outcome" do
    before do
      allow(SomeService).to receive(:call).and_return(failure_outcome)
    end

    it { responds_with_failure_json }
  end
end
Enter fullscreen mode Exit fullscreen mode
tests Article's
30 articles in total
Favicon
Sufficient Software Tests Using Metrics
Favicon
Exploring the Benefits of Integration Testing
Favicon
Best Practices for Effective Automated Integration Tests
Favicon
Automated Tests instrumentation via OpenTelemetry and Aspire Dashboard
Favicon
Integrated tests... are they really important?
Favicon
Focusing on high code coverage can be a trap
Favicon
5 Mistakes to Avoid While Writing Integration Tests
Favicon
Reaching an improved realistic testing approach in the Laravel feature test
Favicon
Desafios Comuns na Escrita de Testes Automatizados: Rumo à Clareza e Padronização - Parte 1
Favicon
Artigo Software Testing: A Research Travelogue - Resumo em PT-BR
Favicon
QA - Définitions et théorie
Favicon
QA - Comment rédiger un test utile ?
Favicon
Applying integration test on NestJS with Jest and GitHub Actions
Favicon
DRY up RSpec subject defining
Favicon
On testing
Favicon
Um vídeo sobre gems e recursos interessantes que podemos integrar com nossas aplicações rails.
Favicon
🩰 Schedule automated tests; become premier ballet artiste
Favicon
Testing Timer-based Logic in Elixir with Klotho Library
Favicon
[Go] How to work with dates in tests
Favicon
Usando o chat do Bing como um aliado para escrever testes de software
Favicon
When and How to Write End-to-End Tests: A Beginner's Guide to Automated E2E Testing
Favicon
Easy Integration Tests for Event-Driven AWS Architectures with EventScout 📨🔭
Favicon
Fix Symfony tests with PHPUnit 10
Favicon
Is programming in TypeScript simply another excuse not to write test in JavaScript?
Favicon
Mocking Interface with jest-mock-extended
Favicon
Do we must implements unit test just to have coverage?
Favicon
Testing a FastAPI application using Ormar models and Alembic migrations
Favicon
How do you deal with test record leaks?
Favicon
Improve your tests with Assert Object Pattern
Favicon
Laravel how to set app environment during tests

Featured ones: