FAQ Database Discussion Community


Run unit test with Ruby 2.0 and minitest 5.5 without Gemfile

ruby,unit-testing,minitest
I was learning Ruby by reading Programming Ruby and there is this example code: require_relative 'count_frequency' require_relative 'words_from_string' require 'test/unit' class TestWordsFromString < Test::Unit::TestCase def test_empty_string assert_equal([], words_from_string('')) assert_equal [], words_from_string(' ') end def test_single_word assert_equal ['cat'], words_from_string('cat') assert_equal ['cat'], words_from_string(' cat ') end def test_many_words assert_equal ['the', 'cat', 'sat',...

How can I test multistep forms with Capybara & Minitest?

ruby,testing,sinatra,capybara,minitest
I have a single page Sinatra app that has a multistep form/wizard interface. If I want to test the form with Capybara, will need to repeat all the steps for each test? I'm hoping to avoid something like this: it "visits the home page" do vist "/" page.should have_content('foo') end...

How do you use multiple tasks using Rake::TestTask?

ruby,testing,rake,minitest,rake-task
I am running Minitest with Rake and would like to have two separate Rake tasks. I have added the following: require 'rake/testtask' task :default => [:test] task :quick => [:unit] Rake::TestTask.new do |t| puts 'within test task' t.libs.push 'specs' t.pattern = 'specs/*_spec.rb' ENV['STACK'] = 'stack1' puts "test stack #{ENV['STACK']}" end...

How do you specify the exact order of Minitest tests?

ruby-on-rails,ruby,testing,minitest,testunit
Minitest lets you run tests in order by overriding test_order to alpha. (You can also use the i_suck_and_my_tests_are_order_dependent! method.) After doing this, how do you control the order the tests are run across multiple files? Is there a way to run some tests from one file and then switch to...

Travis CI is faling to run tests with **/*_test.rb pattern

ruby,rspec,travis-ci,minitest
My micro Ruby project tests works on my machine, on private VM, but fails on Travis CI bulid Project uses both rspec (+rspec-given) and minitest Full trace here: https://travis-ci.org/equivalent/code_katas/jobs/61321482 $ bundle exec rake MiniTest::Unit::TestCase is now Minitest::Test. From /home/travis/.rvm/rubies/ruby 2.1.4/lib/ruby/2.1.0/test/unit/testcase.rb:8:in `<module:Unit>' /home/travis/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/test/unit.rb:676:in `<class:Runner>': undefined method `_run_suite' for class `Test::Unit::Runner'...

minitest test rescue block for Resque::TermException

ruby,exception,resque,minitest,rescue
I am writing tests with minitest for a resque worker. How do I write a test for the rescue block that gets called in case Resque gets terminated and sends a Resque::TermException to the worker? My approach is to stub one of the methods called by the worker and have...

Guard not sending gntp notifications to growl

ruby,guard,minitest,growl
I am using guard with minitest, and everything works great except growl notifications don't work. Gemfile gem 'growl' gem 'guard' gem 'guard-minitest' gem 'json' gem 'minitest' gem 'rack-test' gem 'ruby_gntp' gem 'sinatra' Guardfile guard :minitest do watch(%r{^spec/(.*)_spec\.rb$}) watch(%r{^spec/test_helper.rb$}) watch('beacons_app.rb') { "spec/beacons_app_spec.rb" } end notification :gntp (NOTE: I have tried putting...

Minitest: How to stub/mock the file result of Kernel.open on a URL

ruby,unit-testing,minitest
I have been trying to use Minitest to test my code (full repo) but am having trouble with one method which downloads a SHA1 hash from a .txt file on a website and returns the value. Method: def download_remote_sha1 @log.info('Downloading Elasticsearch SHA1.') @remote_sha1 = '' Kernel.open(@verify_url) do |file| @remote_sha1 =...

Added method to Ruby class throws NoMethodError in MiniTest

ruby,namespaces,metaprogramming,minitest
So why is this happening? It has to be a namespace error, I just don't understand where it is. I add a method to Fixnum like so in a file file.rb module M class Fixnum def foo return true end end end then I'll make a test like so: require...

How to run existing test code on Ruby 2.2

ruby,minitest,ruby-2.2
The following code (which has no Gemfile) works on Ruby 2.1.1, but not Ruby 2.2.0 require "bundler/setup" gem "minitest", "4.7.5" require "test/unit" class TestFoo < Test::Unit::TestCase def test_foo assert true, "Useless mesage" skip "Skip works" end end On Ruby 2.1.1, I get Run options: # Running tests: [1/1] TestFoo#test_foo =...

Shoulda route matcher with subdomains

minitest,shoulda
I am using shoulda matchers to test critical routes on Rails 4.2.1 with Ruby 2.2.0 on a new application. I just moved my API namespace to a subdomain, and I can't figure out how to get the shoulda routes matcher (or any other concise routes test) to work. Here is...

get value of instance var controller method being tested

ruby-on-rails,testing,minitest
I have a method that essentially clones a record. Then redirects to the cloned records edit page. I have a mini test that needs to test that the record gets cloned and redirected to the new records edit path. @new_record is a new object created in the controller method describe...

Route Generation error for :create action in Minitest

ruby-on-rails,routes,params,minitest
I am currently writing functional tests for all of my controllers. For every single one, I can't get the create action to work. I keep getting the following error when I run rake test: ActionController::UrlGenerationError: No route matches {:action=>"create", :comment=>{:content=>"I'm a comment.", :product_id=>"685617403"}, :controller=>comments} Here is the action I am...

Minitest Issues Ruby on Rails

ruby-on-rails,ruby,rake,minitest
I'm reading the Ruby on Rails Tutorial by Michael Hartl and following along. I'm having problems however right when trying to do some testing. [[email protected] twitter-clone]$ bundle exec rake test /media/Data.II/Dropbox/Web Development/odin-project/3. Ruby on Rails/twitter-clone/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do...

Minitest/Capybara/Devise login is not progressing past sign in page

ruby-on-rails,devise,capybara,minitest
Following some information from this example: http://snippets.aktagon.com/snippets/644-how-to-test-authentication-with-devise-capybara-minitest I have the test half running. But the click_on method isn't submitting the form. I've also tried click_button 'Sign in' and page.execute_script("$('form').submit()") without any luck. Here' the test: # test/integration/login_test.rb require "test_helper" require "minitest/rails/capybara" class LoginTest < ActionDispatch::IntegrationTest include Rails.application.routes.url_helpers include...

How do I verify the number of elements and content of an array using ParameterMatchers?

ruby,mocha,minitest
I am learning Ruby with TDD using Mocha and MiniTest. I have a class that has one public method and many private methods, so the only method my tests are going to tests are the public one. This public method does some processing and creates an array which is sent...

RailsTutorial Ch 9 Test fail on pagination and delete link

ruby,ruby-on-rails-4,railstutorial.org,will-paginate,minitest
Going through rails tutorial and stuck on chapter 9 for this last error. FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 1.799453] test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1.80s) <delete> expected but was <User 19>.. Expected 0 to be >= 1. test/integration/users_index_test.rb:18:in `block (2 levels) in <class:UsersIndexTest>' test/integration/users_index_test.rb:15:in `block in <class:UsersIndexTest>' Here is my test block from users_index_test.rb...

How to trigger a JS controller action with Minitest

javascript,ruby-on-rails,integration-testing,minitest
In a test I want to simulate a request that only accepts JS requests. I tried it in the "normal" style: patch study_update_user_vocab_path(@user, @vocabs[index]), { strength: "1", resume_study_index: "index" } Here, "strength" and "resume_study_index" are the ids of the form fields. When running the test I get the following error...

Getting an Error in Rails Model Test - Expected “3” Actual 3

ruby-on-rails,ruby,minitest
I've got this test in my Rails app's test/models folder: test "has quantity" do @i.quantity = 3 assert_equal @i.quantity, 3 end I'm getting the following error: Expected: "3" Actual: 3 I'm setting the value as an integer (and the database column is set to integer). Here's the corresponding migration: class...

Rails4 Minitest | using a shared reusable object

ruby-on-rails,ruby,ruby-on-rails-4,tdd,minitest
I am getting the following error when I run rake test with minitest: $ rake test DL is deprecated, please use Fiddle Run options: --seed 55196 # Running: .EE Finished in 0.950979s, 3.1546 runs/s, 1.0515 assertions/s. 1) Error: CategoryTest#test_invalid_without_name: NoMethodError: undefined method `name=' for nil:NilClass test/models/category_test.rb:14:in `test_invalid_without_name' 2) Error: CategoryTest#test_invalid_without_long_name:...

Why does Minitest seem to not always report what the 'Actual' results of the test are?

ruby,minitest
Why does MiniTest sometimes truly display what's 'Actual' and sometimes it will just show a nil/blank/quote. Example - I know that the value of a.join has "Hi" displayed on 2 lines with a blank line. class Question def stack "Hi\n" \ "Hi\n" end def overflow a = [] a <<...

How to compare all of the elements in an array using assert_equal in MiniTest

ruby,minitest
I am comparing the values in the array, but when the condition fails, it exits. Here I wanted to compare all the elements of the array irrespective of pass and fail: val.each do | x | #assert_equal 48000.00, x assert(48000.00 == x, message = " :Pass") end Suppose the array...

RoR Tutorial Chapter 3 - Guard detects changes but doesn't fully execute tests

ruby-on-rails,railstutorial.org,minitest,guard
I'm currently following the book and the video and in the video, Hartl runs a guardfile so that it automatically runs tests after any changes have been made. So in the videos when he presses return whilst in guard mode, it says: 17:35:31 - INFO - Running: test/controllers/static_pages_controller_test.rb followed by...

Why are there 2 kinds of controller tests in MiniTest?

ruby-on-rails-4,minitest,ruby-on-rails-4.2
I'm converting over to MiniTest from RSpec, and having a couple of difficulties doing so. I have been following some examples I have found: class ArticlesControllerTest < ActionController::TestCase test "should get index" do get :index assert_response :success assert_not_nil assigns(:articles) end end So that's a class that inherits from ActionController::TestCase, that...

How to regenerate config/application.rb in Rails 4.2?

ruby-on-rails,rspec-rails,minitest,ruby-on-rails-4.2,rails-generate
Does Rails 4.2 provide a single command that regenerates config/application.rb in an existing application? The reason I ask is imagine a Rails app is rails new-ed without the --skip-test-unit option. Then at a much later date, this app is switched to RSpec. How can config/application.rb be regenerated as if the...

Undefined local variable or method `login_path'

ruby-on-rails,path,integration-testing,minitest
I'm doing some integration testing in rails for a sessions controller and in the test I get that my login_path is not defined even though it is defined in the routes.rb file and when I invoke that path in the console it also shows up. Any idea what's causing this?...

Set request variables for functional testing of controllers in rails with minitest

ruby-on-rails,ruby-on-rails-4,request,minitest
When doing functional tests for controllers in rails how can I provide dynamic application instance variables to my test which live in the request. I have a @station object that is initialized in a before action in my application controller (available in all other controllers), however the @station object is...

Minitest::Mock and case statements

ruby,mocking,minitest
Any idea why the following code does not work? require 'minitest/mock' test = Minitest::Mock.new test.expect :class, Class p case test when Class 'class' else 'nope' end # => 'nope' … but should return 'class' p test.class === Class # => true ...

How to stub multiple calls to same function to get different returns using minitest

ruby,mocking,minitest,stubbing
I am trying to stub a class method. The method has multiple calls to 'gets'. I wanted to manually return specific values to 'gets' calls. My code is similar to this - ParseAndPlay.stub :gets, {***NEED SOMETHING HERE***} do ParseAndPlay.run end and ParseAndPlay.run expects def ParseAndPlay.run gets #=> expects "5 5"...

Idiomatically mock OpenURI.open_uri with Minitest

ruby,mocking,minitest
I have code that invokes OpenURI.open_uri and I want to confirm the URI being used in the call (so a stub isn't going to work for me), but also intercept the call. I'm hoping not to have to abstract away the call to OpenURI.open_uri just for test purposes. What I've...