Skip to content

Instantly share code, notes, and snippets.

describe Company do
before { @company = Company.new }
describe "when address2" do
it_behaves_like "a text field", "address2", "a", Company.address2.limit do
let(:model) { @company }
end
end
end
@myronmarston
myronmarston / how.md
Created February 18, 2013 03:32
How does SimpleDelegator#method work?

How in the world does SimpleDelegator#method work?

  • SimpleDelegator subclasses BasicObject, which does not provide a method method.
  • Looking over the source, you can see that it doesn't define a method method anywhere.
  • It does define method_missing and proxy undefined methods to the delegated object...but I don't think that can be how it works, because both of these examples work:
@myronmarston
myronmarston / send usages
Created February 13, 2013 04:28
Usages of `send` in rspec-expectations
➜ lib git:(master) ack send
rspec/matchers/built_in/be.rb
102: @actual.__send__ @operator, @expected
139: return @result = actual.__send__(predicate, *@args, &@block)
145: return @result = actual.__send__(present_tense_predicate, *@args, &@block)
rspec/matchers/built_in/change.rb
7: @value_proc = block || lambda {receiver.__send__(message)}
rspec/matchers/built_in/exist.rb
@myronmarston
myronmarston / what_status_code.md
Created February 8, 2013 18:30
What status code should we return for this situation?

What status code should this return?

I'm working on an HTTP-compliant API for a service. We've got a situation where we're not sure what status code to return.

In our system, users create "campaigns" to track data they care about. From the point they create a campaign, it can take a while (e.g. multiple days) before the first batch of data is ready.

During this time before the first batch is ready, when a client

@myronmarston
myronmarston / my_class_spec.rb
Last active December 11, 2015 14:58
Demonstration of how to override the default implicit subject in rspec.
class MyClass
def initialize
raise "Initialize was called"
end
end
# Declare a shared_context that is included in all example groups
# (using `:example_group => { :file_path => // }` to match all
# example groups.
shared_context "using allocate instead of initalize", :example_group => { :file_path => // } do
@myronmarston
myronmarston / explanation.md
Last active October 22, 2020 18:16
Explanation for why `its` will be removed from rspec-3

its isn't core to RSpec. One the of the focuses of RSpec is on the documentation aspects of tests. Unfortunately, its often leads to documentation output that is essentially lies. Consider this spec:

User = Struct.new(:name, :email)

describe User do
  subject { User.new("bob") }
  its(:name) { should == "bob" }
end
CREATE TABLE `engines` (
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5249 DEFAULT CHARSET=utf8mb4;
INSERT INTO `engines` (`name`) VALUES ("~other~"), ("bing"), ("google");
SELECT * FROM `engines` ORDER BY `name` ASC;
module Hamster
def self.vector(*args)
Vector.new(args)
end
class Vector < ::Array
alias add <<
end
end
@myronmarston
myronmarston / dataset_wrapper.rb
Created December 12, 2012 15:52
Playing around with sequel dataset wrapping.
require 'forwardable'
module TopRankingKeyword
class DatasetWrapper
def initialize(ds)
@ds = ds
end
extend Forwardable
def_delegators :@ds, :sql
# A sample Gemfile
source "http://rubygems.org"
gem 'vcr'
gem 'fakefs'
gem 'typhoeus'
gem 'fakeweb'
gem 'rspec'
gem 'rr'