Skip to content

Instantly share code, notes, and snippets.

View dchelimsky's full-sized avatar

David Chelimsky dchelimsky

  • Retired
  • Chicago, IL, USA
View GitHub Profile
# Note the differences between this and https://gist.github.com/2938822 are that
# we're making 10k groups with 1 example each vs 1 group w/ 10k examples, and the
# rspec example uses "should eq" instead of "should =="
if $0 =~ /rspec$/
10_000.times do |i|
describe "loltest #{i}" do
it "does #{i}" do
i.should eq i
end
@dchelimsky
dchelimsky / 1_let.rb
Created March 29, 2012 23:33 — forked from ryanb/1_let.rb
let vs def
desc "A user's comment" do
let(:user) { User.create! name: "John" }
let(:comment) { user.comments.create! }
it "delegates to user's name" do
comment.name.should eq(user.name)
end
end
describe "GET /agents.json" do
it_behaves_like "API controller", :params => { ... }, :environment => { ... }
end
@dchelimsky
dchelimsky / output.txt
Created August 6, 2011 15:12 — forked from cjbottaro/gist:1128091
using shared_context with a block
# The following is an example of how you might use shared content to reduce the
# duplication in https://gist.github.com/1128108. I prefer to avoid examples
# that use `should respond_to`, as it specifies structure rather than behavior.
# I'd recommend specifying something about what happens when the message is
# sent, e.g. subject.some_method.should eq(some_value). That implicitly
# specifies that the object responds to some_method, and provides more useful
# direction/information about how the object should behave.
#
# That said ...
@dchelimsky
dchelimsky / subject_in_each_context_spec.rb
Created May 15, 2011 13:37 — forked from subdigital/better_card_spec.rb
Better rspec structure with subject & let blocks
describe Card do
describe "#value" do
context "Two of Hearts" do
subject { Card.new("2H") }
its(:value) { should == 2 }
end
describe "Face Cards" do
context "King of Clubs" do
describe Subscription do
describe '#api_representation' do
let(:record) { new_valid_record }
before(:each) do
record.engine_variants.first.should respond_to(:api_representation)
record.engine_variants.first.stub(api_representation: 'ev_api_representation')
record.url_fragments.first.should respond_to(:api_representation)
record.url_fragments.first.stub(api_representation: 'uf_api_representation')
# -------------------------------------
# DECLARATION
# using strings for explicit inclusion
shared 'logged in as admin' do
before { login_as admin_user }
end
shared 'a model with a slug' do
it 'sets the slug column upon initialization' do
require 'spec_helper'
describe ArticlesController, :type => :controller do
# ^^ :type => :controller is not necessary if the file is in spec/controllers
describe "GET index" do
# ^^ The describe method creates an example group. ^^
get :index
# ^^ The get method is exposed to an example.
response.should be_successful
end
module MyModule
def some_method
super
rescue SystemStackError => e
puts "you're including MyModule more than once"
end
end
class MyBaseClass; end
describe Controller do
context "create" do
it "saves the model" do
model = Model.new
Model.stub(:new).and_return(model)
model.should_receive(:save)
post :create
end
end
end