Skip to content

Conversation

@britishtea
Copy link
Contributor

To be consistent with assert_raise (which returns its exception), make assert and assert_equal return a value more informative than nil.

  • assert will return the value it has been passed.
  • assert_equal will return true. It can't return a more useful value, because 1 might be equal to 2 if #== has been redefined.

Both assert and assert_equal used to return nil
@djanowski
Copy link
Owner

assert_raise returns the exception because it's convenient when you want to further inspect the exception object. What would be the use case here?

@britishtea
Copy link
Contributor Author

My use case is similar. I depend on the return value in a library that makes it convenient to run multiple tests with random input to test if some property holds.

Stig.property(String) do |random_string|
  assert MyLibrary.takes(random_string)
end

As long as the result of the block is truthy for all generated values, a property is considered held. If the result of the block is falsy, a property is considered not held and an exception is raised. Unfortunately, this happens when I stick a cutest assertion in the block. I prefer to use cutest's assertions for the visual feedback. Depending on a truthy/falsy value also keeps the library test-framework agnostic.

Currently, with cutest I'd have to write a wrapper function like below or lose the visual feedback.

def assert_property(*args, &block)
  Stig.property(args) { |*block_args| assert block.call(block_args) }
end

assert_property(String) do |random_string|
  MyLibrary.takes(random_string)
end

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants