Skip to content

Instantly share code, notes, and snippets.

@JonRowe
Last active February 11, 2016 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonRowe/09d14db49df8708f659e to your computer and use it in GitHub Desktop.
Save JonRowe/09d14db49df8708f659e to your computer and use it in GitHub Desktop.
RubyConfAU16 Code
-r ./talk_helper
require 'test/unit'
class TC_MyTest < Test::Unit::TestCase
SPEED_OF_LIGHT = 299792458
def setup
@e = 898755178736817640
@m = 10
@c
end
def teardown
@e = nil
@m = nil
end
def c
SPEED_OF_LIGHT
end
def test_that_e_equals_mc_squared
assert_equal(@e, @m*(c**2))
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
let(:m) { 10 }
let(:einstein) { AlbertEinstein.new }
it 'says that e=mc²' do
RSpec::Mocks::ExpectationTarget.new(einstein)
. to receive(:theorise).with(m*(speed_of_light**2)) { e }
einstein.lecture
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
let(:m) { 10 }
let(:einstein) { AlbertEinstein.new }
it 'says that e=mc²' do
RSpec::Mocks::ExpectationTarget.new(einstein)
. to ::RSpec::Mocks::Matchers::Receive.new(:theorise, proc { e })
.with(m*(speed_of_light**2))
einstein.lecture
end
end
module RSpec
module Core
module MockingAdapters
module MyFramework
include ::RSpec::Mocks::ExampleMethods
def self.framework_name
:my_framework
end
def self.configuration
end
def setup_mocks_for_rspec
end
def verify_mocks_for_rspec
end
def teardown_mocks_for_rspec
end
end
end
end
end
# Sets up the different example group modules for the different spec types
#
# @api private
def self.add_test_type_configurations(config)
config.include RSpec::Rails::ControllerExampleGroup, :type => :controller
config.include RSpec::Rails::HelperExampleGroup, :type => :helper
config.include RSpec::Rails::ModelExampleGroup, :type => :model
config.include RSpec::Rails::RequestExampleGroup, :type => :request
config.include RSpec::Rails::RoutingExampleGroup, :type => :routing
config.include RSpec::Rails::ViewExampleGroup, :type => :view
config.include RSpec::Rails::FeatureExampleGroup, :type => :feature
end
module RSpec
module Rails
# @api public
# Container class for request spec functionality.
module RequestExampleGroup
extend ActiveSupport::Concern
include RSpec::Rails::RailsExampleGroup
include ActionDispatch::Integration::Runner
include ActionDispatch::Assertions
include RSpec::Rails::Matchers::RedirectTo
include RSpec::Rails::Matchers::RenderTemplate
include ActionController::TemplateAssertions
# Delegates to `Rails.application`.
def app
::Rails.application
end
included do
before do
@routes = ::Rails.application.routes
end
end
end
end
end
module RSpec
module Rails
module Matchers
# Matcher for template rendering.
module RenderTemplate
# @private
class RenderTemplateMatcher < RSpec::Matchers::BuiltIn::BaseMatcher
def initialize(scope, expected, message = nil)
@expected = Symbol === expected ? expected.to_s : expected
@message = message
@scope = scope
@redirect_is = nil
end
# @api private
def matches?(*)
match_check = match_unless_raises ActiveSupport::TestCase::Assertion do
@scope.assert_template expected, @message
end
check_redirect unless match_check
match_check
end
RSpec.configure do |config|
config.expect_with :test_unit
end
RSpec.describe [1] do
it "is equal to [1]" do
assert_equal [1], [1], "expected [1] to equal [1]"
end
specify { assert_not_equal [1], [] }
it "is equal to [2] (intentional failure)" do
assert [1] == [2], "errantly expected [2] to equal [1]"
end
end
require 'minitest/autorun'; require 'rspec/mocks'
class RSpecMocksTest < Minitest::Test
include ::RSpec::Mocks::ExampleMethods
def before_setup
::RSpec::Mocks.setup
end
def after_teardown
::RSpec::Mocks.verify
ensure
::RSpec::Mocks.teardown
end
def test_passing_positive_expectation
dbl = double
expect(dbl).to receive(:message)
dbl.message
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
before do
@m = 10
end
after do
@m = nil
end
it 'says that e=mc²' do
fail 'E!=mc²' unless (e == @m*(speed_of_light**2))
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
before do
@m = 10
end
after do
@m = nil
end
it 'says that e=mc²' do
fail 'E!=mc²' unless (e == @m*(speed_of_light**2))
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
include TheoryHelpers
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
before do
@m = 10
end
after do
@m = nil
end
it 'says that e=mc²' do
prove 'E!=mc²', (e == @m*(speed_of_light**2))
end
end
RSpec.configure do |config|
config.include TheoryHelpers, scientific: :theories
end
class EinsteinsTheoryOfRelativity < RSpec::Core::ExampleGroup
def speed_of_light
299792458
end
def e
898755178736817640
end
before { @m = 10 }
after { @m = nil }
with_replaced_metadata({}) do
RSpec::Core::Example.new(self, 'says that e=mc²', {}, proc do
fail 'E!=mc²' unless (e == @m*(speed_of_light**2))
end)
end
end
# EinsteinsTheories < RSpec::Core::ExampleGroup
RSpec.describe 'Einsteins Theories' do
# RelativityTheory < EinsteinsTheories
describe 'Relativity Theory' do
# Example.new(#<RelativityTheory:...>)
it 'says that e=mc²'
end
# UnifiedTheory < EinsteinsTheories
describe 'Unified Theory' do
# TheoryOfEverything < UnifiedTheory
describe 'TheoryOfEverything'
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
before do
@m = 10
end
after do
@m = nil
end
it 'says that e=mc²' do
# expect(e).to eq @m*(speed_of_light**2)
::RSpec::Expectations::ExpectationTarget.for(e, nil).
to eq @m*(speed_of_light**2)
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
before do
@m = 10
end
after do
@m = nil
end
it 'says that e=mc²' do
# expect(e).to eq @m*(speed_of_light**2)
::RSpec::Expectations::ExpectationTarget.for(e, nil).
to RSpec::Matchers::BuiltIn::Eq.new(@m*(speed_of_light**2))
end
end
RSpec::Matchers.define :eq_mc² do |c:|
match do |e|
e == m*(c**2)
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
let(:m) { 10 }
it 'says that e=mc²' do
expect(e).to eq_mc² c: speed_of_light
end
end
class EqualsMC²
def initialize c:, m:
@c = c
@m = m
end
attr_reader :c, :m
def description
'Einsteins Theory of Relativity'
end
def matches? e
e == m*(c**2)
end
def failure_message
'did not equal mc²'
end
end
require './8-2-matcher_spec.rb'
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
let(:m) { 10 }
it 'says that e=mc²' do
expect(e).to EqualsMC².new c: speed_of_light, m: m
end
end
module RSpec
module Matchers
# rspec-expectations can work with any matcher object that implements this protocol.
#
# @note This class is not loaded at runtime by rspec-expectations. It exists
# purely to provide documentation for the matcher protocol.
class MatcherProtocol
# @!group Required Methods
# @!method matches?(actual)
# @param actual [Object] The object being matched against.
# @yield For an expression like `expect(x).to matcher do...end`, the `do/end`
# block binds to `to`. It passes that block, if there is one, on to this method.
# @return [Boolean] true if this matcher matches the provided object.
# @!method failure_message
# This will only be called if {#matches?} returns false.
# @return [String] Explanation for the failure.
# @!endgroup
# @!group Optional Methods
# @!method does_not_match?(actual)
# In a negative expectation such as `expect(x).not_to foo`, RSpec will
# call `foo.does_not_match?(x)` if this method is defined. If it's not
# defined it will fall back to using `!foo.matches?(x)`. This allows you
# to provide custom logic for the negative case.
#
# @param actual [Object] The object being matched against.
# @yield For an expression like `expect(x).not_to matcher do...end`, the `do/end`
# block binds to `not_to`. It passes that block, if there is one, on to this method.
# @return [Boolean] true if this matcher does not match the provided object.
# @!method failure_message_when_negated
# This will only be called when a negative match fails.
# @return [String] Explanation for the failure.
# @note This method is listed as optional because matchers do not have to
# support negation. But if your matcher does support negation, this is a
# required method -- otherwise, you'll get a `NoMethodError`.
# @!method description
# The description is used for two things:
#
# * When using RSpec's one-liner syntax
# (e.g. `it { is_expected.to matcher }`), the description
# is used to generate the example's doc string since you
# have not provided one.
# * In a composed matcher expression, the description is used
# as part of the failure message (and description) of the outer
# matcher.
#
# @return [String] Description of the matcher.
# @!method supports_block_expectations?
# Indicates that this matcher can be used in a block expectation expression,
# such as `expect { foo }.to raise_error`. Generally speaking, this is
# only needed for matchers which operate on a side effect of a block, rather
# than on a particular object.
# @return [Boolean] true if this matcher can be used in block expressions.
# @note If not defined, RSpec assumes a value of `false` for this method.
# @!method expects_call_stack_jump?
# Indicates that when this matcher is used in a block expectation
# expression, it expects the block to use a ruby construct that causes
# a call stack jump (such as raising an error or throwing a symbol).
#
# This is used internally for compound block expressions, as matchers
# which expect call stack jumps must be treated with care to work properly.
#
# @return [Boolean] true if the matcher expects a call stack jump
#
# @note This method is very rarely used or needed.
# @note If not defined, RSpec assumes a value of `false` for this method.
# @!method diffable?
# @return [Boolean] true if `actual` and `expected` can be diffed.
# Indicates that this matcher provides `actual` and `expected` attributes,
# and that the values returned by these can be usefully diffed, which can
# be included in the output.
# @!method actual
# @return [String, Object] If an object (rather than a string) is provided,
# RSpec will use the `pp` library to convert it to multi-line output in
# order to diff.
# The actual value for the purposes of a diff.
# @note This method is required if `diffable?` returns true.
# @!method expected
# @return [String, Object] If an object (rather than a string) is provided,
# RSpec will use the `pp` library to convert it to multi-line output in
# order to diff.
# The expected value for the purposes of a diff.
# @note This method is required if `diffable?` returns true.
# @!endgroup
end
end
end
RSpec.describe 'Einsteins Theory of Relativity' do
let(:speed_of_light) { 299792458 }
let(:e) { 898755178736817640 }
let(:m) { 10 }
let(:einstein) { AlbertEinstein.new }
it 'says that e=mc²' do
expect(einstein).to receive(:theorise).with(m*(speed_of_light**2)) { e }
einstein.lecture
end
end
module TheoryHelpers
def prove theory, result
fail theory unless result
end
end
class AlbertEinstein
def lecture
theorise 898755178736817640
end
end
class ActiveSupport
module Concern
end
end
class ActionDispatch
module Assertions
end
module Integration
module Runner
end
end
end
class ActionController
module TemplateAssertions
end
end
module RSpec
module Rails
module RailsExampleGroup
def self.included *args
end
end
module Matchers
module RedirectTo
end
module RenderTemplate
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment