Awesome
AnyValue
AnyValue is a collection of helper methods like: anything
, any_number
, any_string
that is useful for testing nested data structures (like arrays or hashes) when you care more about some particular elements and the "shape" of the data, than about the entire data structure.
without this gem:
# Asserting all elements, even the attributes you don't really care about (ids, created_at fields etc)
def test_create
item1 = Item.create!(name: "Item 1")
item2 = Item.create!(name: "Item 2")
get "/items"
assert_equal [
{"id" => item1.id, "name" => "Item 1"},
{"id" => item2.id, "name" => "Item 2"},
], JSON(response.body)
end
# Extracting out subset of attributes you care about:
def test_create
item1 = Item.create!(name: "Item 1")
item2 = Item.create!(name: "Item 2")
get "/items"
assert_equal [
"Item 1", "Item 2",
], JSON(response.body).map { |h| h['name'] }
end
with this gem:
def test_create
item1 = Item.create!(name: "Item 1")
item2 = Item.create!(name: "Item 2")
get "/items"
assert_equal [
{"id" => any_integer, "name" => "Item 1"},
{"id" => any_integer, "name" => "Item 2"},
], JSON(response.body)
end
Usage
All you have to do is to include AnyValue
module to your program/test/whatever.
Example:
require 'any_value'
require 'minitest/autorun'
class Test < Minitest::Test
include AnyValue
def test_anything
assert_equal any_number, 42
end
end
Composition
You can compose helpers like this:
acronym = upcase_string ^ string_of_length(3)
acronym == "NBA" # => true
acronym == "nba" # => false
acronym == "NASA" # => false
And like that for arrays:
array_of_integers = array_of(any_integer)
array_of_integers == [1, 2, 3] # => true
array_of_integers == [1, nil, 3] # => false
See: https://github.com/wojtekmach/anything/blob/master/test/anything_test.rb for more examples.
Installation
The gem is not released to rubygems.org yet, since this name is taken.
You can install it via bundler by adding gem 'anything', github: 'wojtekmach/anything'
to your Gemfile or by cloning it and running rake install
.
Contributing
- Fork it ( https://github.com/wojtekmach/anything/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request