testing

ruby
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @comment = @post.comments.build(comment_params)

System test: asserting Turbo Stream responses

rails hotwire turbo
by codesnips 4 tabs
rust
use my_crate::add;

#[test]
fn test_public_api() {
    assert_eq!(add(3, 4), 7);
}

Integration tests in tests/ directory

rust testing integration
by Marcus Chen 1 tab
kotlin
package com.example.myapp.data.repository

import com.example.myapp.data.local.PostDao
import com.example.myapp.data.local.PostEntity
import com.example.myapp.data.remote.ApiService
import com.example.myapp.models.Post

Unit testing with JUnit and MockK

kotlin android testing
by Alex Chen 2 tabs
typescript
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { fileURLToPath, URL } from 'node:url';

const resolvePath = (relative: string): string =>
  fileURLToPath(new URL(relative, import.meta.url));

TypeScript path aliases (tsconfig + bundler)

typescript tooling vite
by codesnips 4 tabs
rust
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

#[cfg(test)]
mod tests {

Unit tests with #[test] and assert macros

rust testing
by Marcus Chen 1 tab
ruby
class ExplainAnalyzer
  def initialize(relation, watched_tables:)
    @relation = relation
    @watched_tables = Array(watched_tables).map(&:to_s)
  end

Fast Fail for Missing Indexes (EXPLAIN sanity check)

rails postgres performance
by codesnips 3 tabs
go
package api_test

import (
  "encoding/json"
  "net/http"
  "net/http/httptest"

Table-driven tests for HTTP handlers with httptest

go testing http
by Leah Thompson 1 tab
typescript
import express, { Express, NextFunction, Request, Response } from 'express';
import { userRoutes } from './userRoutes';

function authenticate(req: Request, res: Response, next: NextFunction) {
  const header = req.header('authorization');
  if (!header || !header.startsWith('Bearer ')) {

Testing Express routes with Supertest + Jest

testing express supertest
by codesnips 4 tabs
rust
use std::io::Write;
use tempfile::NamedTempFile;

fn main() -> std::io::Result<()> {
    let mut tmpfile = NamedTempFile::new()?;
    writeln!(tmpfile, "Hello, temp file!")?;

tempfile for safe temporary file creation

rust testing files
by Marcus Chen 1 tab
ruby
FactoryBot.define do
  factory :post do
    association :author, factory: :user
    sequence(:title) { |n| "Post Title #{n}" }
    body { Faker::Lorem.paragraphs(number: 3).join("\n\n") }
    status { :draft }

Rails fixtures vs factories for test data

rails testing fixtures
by Maya Patel 2 tabs
sql
-- Create test database
CREATE DATABASE myapp_test;

-- Test isolation with transactions
/*
beforeEach(async () => {

Database testing strategies and fixtures

testing database-testing fixtures
by Maria Garcia 2 tabs
typescript
import { http, HttpResponse } from 'msw';

export interface Product {
  id: number;
  name: string;
}

MSW for frontend API mocking in tests

testing frontend react
by codesnips 4 tabs