minitest, test::unit, and rails

There is a Test::Unit “compatibility” module that comes with Minitest, so that you can (presumably) use your existing Test::Unit tests as-is. This is probably the Test::Unit module you are seeing. As of rails 3.2.3, generator-created tests include rails/test_help which includes test/unit. The test “something” do syntax is a rails extension. It’s defined in ActiveSupport::Testing::Declarative, which … Read more

How to assert the contents of an array, indifferent of the ordering

TL;DR The most direct way to check this is to sort the arrays before checking their equality. json.map{|d| d[“id”]}.sort.must_equal Database.all.pluck(:id).sort Still here? Okay. Let’s talk about comparing elements in an array. As it is, the map{} is already somewhat irrelevant to the test and adding clutter, I’d prefer to not add even more. Well, that … Read more

Why doesn’t MiniTest::Spec have a wont_raise assertion?

MiniTest does implement assert_nothing_raised in its Test::Unit compatibility layer, but in its own tests (MiniTest::Unit and MiniTest::Spec) it does not implement any test like this. The reason is, the programmer argues, that testing for nothing raised is not a test of anything; you never expect anything to be raised in a test, except when you … Read more

How to use assert_select to test for presence of a given link?

In assert_select, you can also use a question mark for the attribute value, and then follow with a string to match. assert_select “a[href=?]”, “https://stackoverflow.com/desired_path/1” There’s another way to use assert_select that is more friendly, especially if you want to match a partial string or regexp pattern: assert_select “a”, :href => /acebook\.com\/share/

How to assert certain method is called with Ruby minitest framework?

Minitest has a special .expect :call for checking if some method is called. describe SomeClass do it “should invoke right function” do mocked_method = MiniTest::Mock.new mocked_method.expect :call, return_value, [] some_instance = SomeClass.new some_instance.stub :right, mocked_method do some_instance.invoke_function(“right”) end mocked_method.verify end end Unfortunately this feature is not documented very well. I found about it from here: … Read more

How to set up MiniTest?

This question is similar to How to run all tests with minitest? Using Ruby 1.9.3 and Rake 0.9.2.2, given a directory layout like this: Rakefile lib/alpha.rb spec/alpha_spec.rb Here is what alpha_spec.rb might look like: require ‘minitest/spec’ require ‘minitest/autorun’ # arranges for minitest to run (in an exit handler, so it runs last) require ‘alpha’ describe … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)