minitest

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

泪湿孤枕 提交于 2019-11-30 10:53:02
I want to test whether a function invokes other functions properly with minitest Ruby, but I cannot find a proper assert to test from the doc . The source code class SomeClass def invoke_function(name) name == "right" ? right () : wrong () end def right #... end def wrong #... end end The test code: describe SomeClass do it "should invoke right function" do # assert right() is called end it "should invoke other function" do # assert wrong() is called end end With minitest you use expect method to set the expectation for a method to be called on a mock object like so obj = MiniTest::Mock.new

What is the expected syntax for checking exception messages in MiniTest's assert_raises/must_raise?

天涯浪子 提交于 2019-11-30 02:32:45
What is the expected syntax for checking exception messages in MiniTest's assert_raises / must_raise ? I'm trying to make an assertion something like the following, where "Foo" is the expected error message: proc { bar.do_it }.must_raise RuntimeError.new("Foo") You can use the assert_raises assertion, or the must_raise expectation. it "must raise" do assert_raises RuntimeError do bar.do_it end -> { bar.do_it }.must_raise RuntimeError lambda { bar.do_it }.must_raise RuntimeError proc { bar.do_it }.must_raise RuntimeError end If you need to test something on the error object, you can get it from

Using Minitest in Rails

吃可爱长大的小学妹 提交于 2019-11-30 01:46:49
Recently, I've read quite a few articles about Minitest. I really like the idea of a super lightweight test framework. I decided to replace rspec with it in a recent project and have had no luck getting it all to work. My problems are a) getting named routes in my acceptance/integration tests (rspec and test::unit seem to automatically include them but no go with minitest), b) and the overall lack of adoption in rails makes me uneasy (everyone seems to be using rspec though it's used more with gems/libraries). Is it worth using minitest when rspec has the main dominance with testing rails

Is it possible to run a single test in MiniTest?

你。 提交于 2019-11-29 18:49:09
I can run all tests in a single file with: rake test TEST=path/to/test_file.rb However, if I want to run just one test in that file, how would I do it? I'm looking for similar functionality to: rspec path/to/test_file.rb -l 25 I'm looking for similar functionality to rspec path/to/file.rb -l 25 Yup! Use Nick Quaranto's "m" gem . With it you can say: m spec/my_spec.rb:25 jduan The command should be: % rake test TEST=test/test_foobar.rb TESTOPTS="--name=test_foobar1 -v" Andrew Grimm Have you tried: ruby path/to/test_file.rb --name test_method_name This is one of the things that bother me about

Check method call on model using MiniTest

馋奶兔 提交于 2019-11-29 14:46:06
If I was using RSpec I could test if a method is being called like so: expect(obj).to receive(:method) What is the equivalent in MiniTest? I have a model, Post , which has a before_validation callback which runs a method create_slug . In my test test/models/post_test.rb I want to ensure that the create_slug method is being called when calling post.save . The Minitest::Spec documentation says that I can use a method must_send to check if a method is called. However, when I try @post.must_send :create_slug I receive the following error: NoMethodError: undefined method `must_send' for #<Post

Testing a concern / module that uses ActiveRecord

☆樱花仙子☆ 提交于 2019-11-29 08:09:44
问题 SCENARIO I have extracted a concern called Taggable . It's a module that allows any model to support tagging. I have included this concern/module into models like User , Location , Places , Projects . I want to write tests for this module, but don't know where to start. QUESTION 1. Can I do isolation testing on the Taggable concern? In the example below the test fails because the test is looking for a dummy_class table . I'm assuming it's doing this because of the has_many code in Taggable so

How do I stub things in MiniTest?

余生长醉 提交于 2019-11-28 16:18:36
Within my test I want to stub a canned response for any instance of a class. It might look like something like: Book.stubs(:title).any_instance().returns("War and Peace") Then whenever I call @book.title it returns "War and Peace". Is there a way to do this within MiniTest? If yes, can you give me an example code snippet? Or do I need something like mocha? MiniTest does support Mocks but Mocks are overkill for what I need. Panic # Create a mock object book = MiniTest::Mock.new # Set the mock to expect :title, return "War and Piece" # (note that unless we call book.verify, minitest will # not

Is it possible to run a single test in MiniTest?

陌路散爱 提交于 2019-11-28 13:47:45
问题 I can run all tests in a single file with: rake test TEST=path/to/test_file.rb However, if I want to run just one test in that file, how would I do it? I'm looking for similar functionality to: rspec path/to/test_file.rb -l 25 回答1: I'm looking for similar functionality to rspec path/to/file.rb -l 25 Yup! Use Nick Quaranto's "m" gem. With it you can say: m spec/my_spec.rb:25 回答2: The command should be: % rake test TEST=test/test_foobar.rb TESTOPTS="--name=test_foobar1 -v" 回答3: Have you tried:

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

北慕城南 提交于 2019-11-28 09:37:24
Ruby's Test::Unit has assert_nothing_raised . Test::Unit has been replaced by MiniTest . Why don't MiniTest's assertions / expectations have anything parallel to this? For example you can expect must_raise but not wont_raise . 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 are testing for an

Check method call on model using MiniTest

前提是你 提交于 2019-11-28 08:53:45
问题 If I was using RSpec I could test if a method is being called like so: expect(obj).to receive(:method) What is the equivalent in MiniTest? I have a model, Post , which has a before_validation callback which runs a method create_slug . In my test test/models/post_test.rb I want to ensure that the create_slug method is being called when calling post.save . The Minitest::Spec documentation says that I can use a method must_send to check if a method is called. However, when I try @post.must_send