Skip to content

Instantly share code, notes, and snippets.

Created February 4, 2013 07:19
Show Gist options
  • Save anonymous/4705398 to your computer and use it in GitHub Desktop.
Save anonymous/4705398 to your computer and use it in GitHub Desktop.
What a minitest would look like if the test case object passed bare matcher methods in an "it" block to a "subject" in scope. Like RSpec :)
require 'minitest/autorun'
describe Person do
describe 'direct' do
subject { Person.new({pet: true}).pet? }
it { must_be true }
end
describe 'indirect' do
subject { Person.new({pet: true}) }
it { must_be :pet? }
end
end
@elle
Copy link

elle commented Feb 4, 2013

This is using: https://github.com/wojtekmach/valid_attribute

describe Person do
  context 'should have validations' do
    subject { Person.new }

    it { must have_valid(:name).when(sentence) }
    it { wont have_valid(:name).when(nil, '') }
  end
end

The context is defined in my test_helper.rb as:

class MiniTest::Spec
    class << self
      alias context describe
    end
  end

Also have a look at: https://github.com/zenspider/minitest-matchers

And one more option:

describe Person do
  let(:person) { Person.new({pet: true}) }

  it 'has a pet' do
    assert person.pet?
  end
end

@elle
Copy link

elle commented Feb 4, 2013

Actually I think what you are looking for is covered here: http://blog.arvidandersson.se/2012/03/28/minimalicous-testing-in-ruby-1-9

describe Person do
  subject { Person.new("Yukihiro", "Matsumoto") }

  it "has a full name" do
    subject.full_name.must_equal "Yukihiro Matsumoto"
  end
end

And

describe Person do
  subject { Person.new }

  specify { subject.posts.must_be_empty }
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment