Running tests for ruby on rails - ruby-on-rails

I use RubyMine, Windows
I wrote test:
class PostTest < ActiveSupport::TestCase
# Replace this with your real tests.
fixtures :posts
test "the truth" do
#first_posts = posts(:first_posts)
assert #first_posts.title == "Ruby on rails"
end
end
But when I run test with rubyMine ( with bottom button I select "test" and run it)
i get this
C:\Ruby187\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:\Ruby187\bin/rake test
Testing started at 1:44 PM ...
(in D:/Projects/TestProject)
Empty test suite.
0 tests, 0 assertions, 0 failures, 0 errors
Test suite finished: 0.031002 seconds
Errors running test:units!
Empty test suite.
Process finished with exit code 1
E.g my test suite is empty, but test suite has one test: "the truth"
When i run tests from console (ruby post_test.rb), I have
D:\Projects\TestProject\test>ruby unit/post_test.rb
Loaded suite unit/post_test
Started
Ruby on rails
.
Finished in 1.7471 seconds.
1 tests, 1 assertions, 0 failures, 0 errorsWhat's wrong?

This bug RubyMine (build 98.47) http://youtrack.jetbrains.net/issue/RUBY-6158

Related

Rails Minitest inconsistent data after setup

I've been hitting my head with following issue with no luck:
Trying to test that a rake task populates some tables correctly, I have following configuration:
class InitialDataTest < ApplicationSystemTestCase
def setup
DatabaseCleaner.clean_with :truncation, only: %w[centres projects departments]
System::Application.load_tasks
Rake::Task['populate_lookup_tables:commit'].invoke
sign_in users(:test_user)
end
test 'projects are created' do
puts " Projects: #{Project.count}"
end
test 'centres are created' do
puts "Centres: #{Centre.count}"
end
test 'departments are created' do
puts " Departments: #{Department.count}"
end
end
My rake task actually works as expected (in development, for instance) however, for the test it seems that only 1 table is being populated:
Running via Spring preloader in process 15362
Started with run options --seed 17058
:: Centre populated successfully
:: Project populated successfully
:: Department populated successfully
>> OperationType is not empty
>> DocumentType is not empty
>> RecordErrorType is not empty
Puma starting in single mode...
* Version 3.9.1 (ruby 2.3.3-p222), codename: Private Caller
* Min threads: 0, max threads: 1
* Environment: test
* Listening on tcp://0.0.0.0:51272
Use Ctrl-C to stop
Departments: 5
Centres: 0
Projects: 0
3/3[========================================================] 100% Time: 00:00:03, Time: 00:00:03
Finished in 3.88633s
3 tests, 0 assertions, 0 failures, 0 errors, 0 skips
Coverage report generated for Unit Tests to <My Stuff> (28.45%) covered.
By default, Rake task can be only invoked once in a given context. If you want to run it again you need to call Rake::Task['populate_lookup_tables:commit'].reenable first.

Customize minitest messages

Is there a way to customize test reports. Currently I am getting this line at the end of each test:
3 tests, 0 assertions, 0 failures, 0 errors, 0 skips
I want to include test file name to this line.
Minitest has a verbose option that will show the test name and execution time for each test that's run. How you pass the -v flag to Minitest will vary depending on how you're running your tests. See http://chriskottom.com/blog/2014/12/command-line-flags-for-minitest-in-the-raw/ for a sample of verbose output.
Another option would be using minitest-reporters with the SpecReporter by adding it to your Gemfile and putting this in your test helper:
require "minitest/reporters"
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
See https://github.com/kern/minitest-reporters for more info.

Rails extending `rake test` to incorporate custom behaviour

I've got a bunch of files in test/policies, and I've tried to enhance rake test like so:
# lib/tasks/test.rake
namespace :test do
desc "Test Pundit policies"
Rake::TestTask.new(:policies) do |t|
t.libs << 'test'
t.pattern = 'test/policies/*_test.rb'
end
end
Rake::Task["test"].enhance do
Rake::Task["test:policies"].invoke
end
It works great if I run bin/rake test:all, but bin/rake test now only runs the policy tests, and none of my others.
Can anyone advise what I am doing wrong here? In case it's not clear, I want rake test to run all my tests, just like it used to.
UPDATE
Actually, it's kind of working now, but I've noticed that my functional, unit and integration tests do run now, but only if the policy tests all pass. If any of the policy tests fail, then the rest of my test suite fails to run.
And I don't like the output, notice how there are two blocks of test output below:
[vagrant#vagrant-centos-6-4 vagrant]$ bin/rake test
Run options: --seed 54880
# Running:
.........................
Finished in 0.584530s, 42.7694 runs/s, 56.4556 assertions/s.
25 runs, 33 assertions, 0 failures, 0 errors, 0 skips
Run options: --seed 19900
# Running:
.........................................................
Finished in 4.132299s, 51.0612 runs/s, 128.9839 assertions/s.
I'd prefer if the policy tests output was merged into the same output block as that from my other tests. Any ideas, or is this as good as it's going to get?
Someone else asked the same question and I answered it for them. The solution in my case was to change my code from:
Rake::TestTask.new(:policies) do |t|
to:
Rails::TestTask.new(:policies) do |t|
It just works, and fixes all the issues I was having.

Is it possible to enhance the rake test task and merge test results together?

I am writing tests for my sidekiq workers and I want them to run when I type "rake" in the terminal. I have that working - I added the following to my Rakefile:
namespace :test do
Rake::TestTask.new(:workers) do |t|
t.libs << "test"
t.pattern = 'test/workers/**/*_test.rb'
end
end
Rake::Task[:test].enhance ["test:workers"]
When I run rake I get something like this as my output:
Run options: --seed 51172
# Running tests:
SS
Finished tests in 0.005594s, 357.5259 tests/s, 0.0000 assertions/s.
2 tests, 0 assertions, 0 failures, 0 errors, 2 skips
Run options: --seed 17561
# Running tests:
S............................................SSSS..SSSSS......
Finished tests in 2.037526s, 30.4291 tests/s, 45.6436 assertions/s.
62 tests, 93 assertions, 0 failures, 0 errors, 10 skips
The S characters are skips - I haven't finished all of my tests yet. So my question is basically - is there a way to merge the two sets of tests after enhancing? Should I be doing something different than an enhance?
If I'm doing anything blatantly wrong please let me know, and thanks for reading this. And just in case it is needed: Rails 4 w/ Ruby 2.0
Try this, change the following:
Rake::TestTask.new(:workers) do |t|
to this:
Rails::TestTask.new(:workers) do |t|
It's a small change, but fixes the problem for me. It means your entire test suite will run with merged output.
there is a simple workaround for this:
bundle exec rake test:all
if you want to see how rails tasks are created look here

spork-testunit no tests run

New to Ruby so please bear with me... I am using JRuby on Windows, spork-0.9.0.rc9. I am able to use rake test fine but when I try with testunit using testdrb my tests are not run. For example:
testdrb -Itest test\functional\contents_controller_test.rb
Loaded suite contents_controller_test
Started
Finished in 0.113000 seconds.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
<-- Slave(2) run done!
Whereas using rake test:
>rake test TEST=test\functional\contentscontroller_test.rb
c:/dev/apps/jruby-1.6.4/lib/ruby/gems/1.8/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53 warning: already initialized constant WFKV
Loaded suite c:/dev/apps/jruby-1.6.4/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/rake_test_loader
Started
ContentsControllerTest:
PASS should create content (0.19s)
PASS should destroy content (0.03s)
PASS should get edit (0.29s)
PASS should get index (0.22s)
PASS should get new (0.06s)
PASS should show content (0.04s)
PASS should update content (0.05s)
Finished in 0.880000 seconds.
7 tests, 17 assertions, 0 failures, 0 errors, 0 skips

Resources