I'm working on a rails project where I have a function which has to pass a lot of tests (hundreds\\thousands calls to the function in it's unit test). I'm just looking for an easy way to assert the result (for example assert(class.foo(params)>0.6, true) to test foo's result is bigger than 0.6), and maybe have it print the line of the tests which didn't pass.
I'm used to this kind of unit testing from C\\C++ programming, but from looking up online for a similar solution in rails I came up with gems like "MiniTest" where I have to encapsulate each assertion in a function (def \\ assertion \\ end), which triples the code's length and makes the whole process sisyphic and the code unreadable.
The bottom line: I'm looking for a way to create a new instance of my class, i = my_class.new, followed by many assertion: assert(i.foo(params)<0.6, true). If a test fails, it would be nice to be notified about the exact line.
Thanks!
I don't know about MiniTest, but RSpec (another popular testing framework) has the ability to aggregate errors. So you no longer have to adhere to the "one assertion per spec" rule. For example:
RSpec.describe 'my big function' do
it 'does some stuff' do
expect(foo(params)).to be > 0.6
expect(bar(params)).to be < 0.4
end
end
In case of multiple failures, you'll get a detailed report, pointing to exact lines with failed expectations.
For minitest you can do multiple assertions in a check like this
require 'test_helper'
class ListTest < ActiveSupport::TestCase
context "test my object" do
setup do
@my_object = MyObject.new
end
should 'be greater than .6 and title equal to blah' do
assert @my_object.value > 0.6
assert_equal @my_object.title, 'blah'
end
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.