test snippets

Testing sessions with Sinatra and Rack::Test

Tagged session, test, sinatra, rack, cookie, rack::test, mocha  Languages ruby

You need support for testing sessions when, for example, testing authentication. The only way I've managed to get sessions to work with Sinatra and Rack::Test is by going through the whole stack, in other words calling the authentication controller as shown here:

@user = Factory(:user) # create a dummy user
User.expects(:authenticate).with(any_parameters).returns(@user) # make authenticate return the dummy user
post "/sign-in", {:email => @user.email, :password => @user.password} # login to the application and set a session variable.

After this the session populated and we're logged in when accessing the application again:

post "/articles", {:title => 'Sessions suck', :body => '...'}

On a side note, there are two ways of specifying sessions that used to work, but which no longer work with Sinatra 1.0:

get '/', :env => { :session => {:abc => 'adf'} }
get '/', {}, :session => {:abc => 'adf'}
get '/', {}, "rack.session" => {:abc => 'adf'}

The session is always empty.

There are many discussions about sessions and Rack::Test, but not one of them has a solution that works for me: * rack.session variable is missing env under test environment * Sessions with rspec and Rack.Test

Mocha stubbing and mocking

Tagged mocha, stub, mock, test  Languages ruby

Mocking with expectations:

@domain = Domain.new
@domain.errors.add(:name, 'WTF')



MiniTest and "0 tests, 0 assertions, 0 failures, 0 errors"

Tagged minitest, test, ruby  Languages ruby

My test_helper.rb looked like this:

require 'minitest/autorun'
require 'app'

The output when running a unit test was this:

0 tests, 0 assertions, 0 failures, 0 errors

The solution was to move the minitest require:

require 'minitest/autorun'
require 'app'

Now my tests are run again when executing:

ruby test/functional/xxx_controller_test.rb

How to test Sinatra helpers

Tagged request, sinatra, test  Languages ruby

This one depends on a request object (Rack::Request) being present:

class AppHelpersSpec < Test::Unit::TestCase                                            
  include AppHelpers
  include Rack::Test::Methods

  def request
    Rack::Request.new(Rack::MockRequest.env_for '/', {params: {name: 'hohohoe'}})

  def test_helper_method
    res = some_helper_method # call helper method
    assert_equal 'WTF!', res


Ruby's database_cleaner for Python

Tagged database, database_cleaner, python, test  Languages python

A simple version of Ruby s database_cleaner gem for Python:


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

engine = create_engine(
    connect_args={"options": f"-c timezone=utc -csearch_path=public"}
Session = sessionmaker(bind=engine, autoflush=True)


import pytest
from db import Session, engine

# You can also use "autouse"
# @pytest.fixture(autouse=True)
def session():
    connection = engine.connect()
    session = Session(bind=connection)
    transaction = connection.begin()
        yield session


def test_something_with_session(session):
    # Session is automatically inserted by the fixture

Also see: https://docs.sqlalchemy.org/en/14/orm/session_transaction.html#tracking-transaction-state-with-events