Skip to content

Instantly share code, notes, and snippets.

@jeanbza
Last active August 29, 2015 14:19
Show Gist options
  • Save jeanbza/afc3f162d835882a54bc to your computer and use it in GitHub Desktop.
Save jeanbza/afc3f162d835882a54bc to your computer and use it in GitHub Desktop.
Starting with rspec

Writing tests with rspec

  1. Rspec basic layout:
/my_integration_tests
  Gemfile
  /spec
    spec_helper.rb
    /features
      foo_spec.rb
      bar_spec.rb
  1. What do they mean?
  • Gemfile = list of gems (packages) that get installed when you run bundle install from this directory. Ruby way to manage dependencies
  • spec_helper.rb = the file that sets up your test environment
  • foo_spec.rb = every rspec test is suffixed with _spec
  1. Basic rspec test:
require_relative '../spec_helper'

before do
  # Reset db
  ActiveRecord::Base.execute('delete * from users')
end

describe 'using the api'
  context 'when mysql is down' do
    before do
      # Imaginary function
      $MysqlServer.stop
    end
    
    after do
      # Imaginary function
      $MysqlServer.start
    end
  
    it 'returns a 500' do
        resp = HTTParty.get('http://localhost:8080/my_api')
        expect(resp.code).to eq 500
    end
  end
end
  1. Decomposing this example: Variables in ruby
# Limited to the scope you're in
local_variable = 1

# Available to the instance of the class you're in (regardless of scope)
@instance_variable = 2

# Available to any instance of the class (e.g., static in java)
@@class_variable = 3

# Available anywhere in your app
$global_variable = 4
  1. Decomposing this example: Rspec blocks
it 'has some resultant behavior' do
  # The test itself
end

# Container for blocks
describe 'a part of the app, or performing an action' do
  # More blocks
end

# Container for blocks
context 'when some STATE is present' do
  # More blocks - context is same as describe
end

before do
  # Run before each test IN THIS describe/context block
end

after do
  # Run after each test IN THIS describe/context block
end
  1. Using a database with ActiveRecord
require 'active_record'

ActiveRecord::Base.establish_connection(database_config)

## USE ACTIVERECORD MODELS:

class User < ActiveRecord::Base
  self.table_name = 'my_user_table'
end

User.find(1) # select * from my_user_table where id = 1
User.find([1,5,7]) # select * from my_user_table where id in (1,5,7)
User.find_by(:name, 'Bob') # select * from my_user_table where name = 'Bob'
User.create(name: "David", occupation: "Artist") # insert into my_user_table (name, occupation) values ("David", "Artist")

user = User.find_by(name: 'David')
user.name = 'Dave'
user.save

## OR USE ACTIVERECORD TO QUERY:

users = ActiveRecord::Base.connection.exec_query('select * from my_user_table')
ActiveRecord::Base.connection.execute('delete from my_user_table')
  1. Writing simulators with sinatra
class MySimulator < Sinatra::Base
  get '/hi'
    status 200
    'Hello world!'
  end
  
  post '/bye'
    status 201
  end
end

simulator = MySimulator
simulator.run!

Miscellaneous ruby quirks

  • Ruby will return whatever is at the end of a function

    # These are equivalent
    
    def foo
      return 'Hello world!'
    end
    
    def bar
      'Hello world!'
    end
    
  • Ruby has string interpolation

    # These are equivalent
    
    user = 'Bob'
    
    foo = 'Hello ' + user
    bar = "Hello #{user}" # Note the double quotes
    
  • Functions don't require parens

    def foo(bar)
      puts bar
    end
    
    # These are equivalent
    
    foo('hello world')
    foo 'hello world'
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment