Skip to content

Instantly share code, notes, and snippets.

joakimk / gist:8703877
Last active Aug 29, 2015
Support unit testing AR models without loading all of rails
View gist:8703877
require "active_record"
# Also highly recommended:
# require "active_support/dependencies"
# ActiveSupport::Dependencies.autoload_paths += [ "app/models" ]
# you don't need to do manual requires, found it to be just as fast.
# Support unit testing AR models without loading all of rails.
connection_info = YAML.load("config/database.yml")).result)["test"]
joakimk / app_mappers_application_mapper.rb
Created Feb 18, 2014
Using the app/records organization method with minimapper.
View app_mappers_application_mapper.rb
require "minimapper/mapper"
class ApplicationMapper < Minimapper::Mapper
def record_class
record_name =, "Record")

I just read

I fully agree that the testing speed can often be a result of improving the code design.

This is probably how I'd implement a similar feature in's apps.

I'm mostly of the opinion that you should use the right tool for each job. Integrated tests to test integration. Unit tests to test logic and message passing.

I prefer to keep things simple and avoid dependency injection until there is a need for it outside of tests.

joakimk / test_helper.exs
Created Aug 12, 2014
Starting a redis server when running elixir tests
View test_helper.exs
Code.require_file "support/test_redis.exs", __DIR__
joakimk / gist:5efe8d04e526d521e8c6
Last active Aug 29, 2015
Alternate heroku setup for circle
View gist:5efe8d04e526d521e8c6
echo -e "machine\n login $HEROKU_USER\n password $HEROKU_API_KEY\nmachine\n login $HEROKU_USER\n password $HEROKU_API_KEY" > ~/.netrc && chmod 0600 /home/ubuntu/.netrc # We want to simply configure the API key, this seems easier than using the regular UI as that requires you to log in as a github user and assign yourself, etc.
joakimk /
Created May 28, 2015
The terminal aliases and functions I somewhat regularly use (gist for easy use when pairing)
# Rails
alias sc='bundle exec rails console'
alias ss="bundle exec rails server"
alias rs='rake spec'
alias rsu='rake spec:unit'
alias rsua='rake spec:unit:all'
alias rsa='rake spec:all'
alias mig='rake db:migrate'
alias rsp='rake testbot:spec'
alias ref='script/refresh'
joakimk /
Last active Aug 29, 2015
Nginx "Bad Request" 400 error because of % in the body text?

If you have a body like this:

payload={"data": "5 %"}

Then nginx will most likely give you a 400 bad request error.

To get around this, encode the json with "x-www-urlencoded" encoding.

A web framework like rails will transparently decode "x-www-urlencoded" for you.

joakimk / code.exs
Last active Aug 29, 2015
Simple example of using GenEvent to pass on messages
View code.exs
# In this example we're running jobs of some kind, and waiting for events if they finish okay or fail.
defmodule JobEvent do
def start_link do
{:ok, pid} = GenEvent.start_link(name: __MODULE__)
defmodule MessageForwarder do
use GenEvent
View gist:89810
#!/usr/bin/env ruby
require 'net/http'
require 'rexml/document'
@cc_server = 'your-cc-server'
include REXML
def build_green?
result = Net::HTTP.start(@cc_server, 8000) { |http| http.get('/XmlStatusReport.aspx') }
joakimk / Testbot stats (for rspec)
Created Jan 26, 2010
Example test runtimes using testbot
View Testbot stats (for rspec)
1 computers - 1 cpus: 29 minutes (rake spec)
5 computers - 13 cpus: 4 minutes (rake testbot:spec)
10 computers - 25 cpus: 2 minutes (rake testbot:spec) (about 60% cpu efficiency)
The testbot communication overhead is about 2-5 seconds per run. Fetching the code
and setting up the test environment takes a bit longer. The jobs are not being
perfectly balanced. There is still plenty of room for optimizations.