1

I have classes in my app that don't really fit the typical Rails file structure. Based on Yehuda Katz's advice in this Stackoverflow answer, I decided to make an app/classes folder. I'd like to test these classes, so I also made a test/classes folder.

My problem is that I can't seem to get the test runner to run the tests in test/classes - how can I get it to do so?

I have app/classes/card.rb and test/classes/card_spec.rb. Here is what is currently happening:

code/poker_analyzer [master●] » test
code/poker_analyzer [master●] » test test/classes/card_spec.rb
code/poker_analyzer [master●] » bin/rails test
Running via Spring preloader in process 8991
Run options: --seed 14207

# Running:



Finished in 0.000522s, 0.0000 runs/s, 0.0000 assertions/s.
0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
code/poker_analyzer [master●] » bin/rails test test/classes/card_spec.rb
Running via Spring preloader in process 9005
Run options: --seed 33782

# Running:

.

Finished in 0.001655s, 604.2296 runs/s, 604.2296 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
code/poker_analyzer [master●] »

So it seems to only work when I run bin/rails test test/classes/card_spec.rb. How can I get it to run in the other scenarios?

Update: It is only supposed to work in the scenarios prefaced by bin/rails. So then, my question is why bin/rails test doesn't work.

Adam Zerner
  • 17,797
  • 15
  • 90
  • 156
  • Check out this blog http://blog.bigbinary.com/2014/04/26/adding-directory-to-rake-test.html – George Jun 29 '17 at 05:12
  • this could also be helpful https://medium.com/@mario_chavez/testing-rails-with-minitest-7b4f99d4fcb8 – George Jun 29 '17 at 05:15
  • @George The first blog post is somewhat useful as a hack, but I'm looking for the approach that follows whatever Convention Over Configuration is currently available. I didn't see an answer in the second article, and it wasn't explicitly for Rails 5. – Adam Zerner Jun 29 '17 at 06:57
  • according to the minitest docs, you should be using a rake task like `bin/rails test` to run your test suite https://github.com/seattlerb/minitest#running-your-tests – George Jun 29 '17 at 14:17
  • @George something seems off if `test` works for things in the default folders, but doesn't work for things in the non-default folders. I assume there is some Rails convention to make it work for non-default folders too. Otherwise, I agree that the best answer is to just run `bin/rails test`, which works when I changed the file names to `_test` like you pointed out. – Adam Zerner Jun 29 '17 at 16:53
  • 1
    does `test` work for the things in default folders? in the code snipped you posted it doesn't look like `test` is doing anything – George Jun 29 '17 at 18:25
  • @George No... it doesn't. My bad! I'm new to rails and was assuming for some reason that `test` is supposed to work, and that the reason it wasn't working is because my app doesn't currently have anything except `app/classes` stuff. My only question then is why `bin/rails test` wasn't working, but you answered that by saying that files need to have the `_test` suffix. I updated my OP to clarify. Thanks. – Adam Zerner Jun 29 '17 at 18:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147976/discussion-between-george-and-adam-zerner). – George Jun 29 '17 at 18:38

1 Answers1

1

I've only used RSpec so this is kind of speculation, but I'm pretty sure the suffix of your test/classes/card_spec.rb file has to be _test not _spec for it to be picked up automatically by the runner.

So change the name totest/classes/card_test.rb and it should work.

George
  • 680
  • 4
  • 16
  • It works when I run `bin/rails test`, but not when I run `test`. – Adam Zerner Jun 29 '17 at 05:07
  • Is this documented anywhere? – Adam Zerner Jun 29 '17 at 05:07
  • @Adam, I'm going to delete my answer -- I don't think it's correct. I couldn't find documentation anywhere. – George Jun 29 '17 at 05:11
  • Or I'm confused, did it work for you? because `test` without bin/rails in front of it is a bash command https://ss64.com/bash/test.html – George Jun 29 '17 at 05:17
  • I wouldn't say that it "worked". `test` is supposed to run all of your tests, and it isn't. – Adam Zerner Jun 29 '17 at 05:20
  • 1
    I don't think there is a `test` command that's supposed to run all of your tests. `test` without `bin/rails` or `rake` in front of it is a system tool that I linked before (type `man test` in your command line and you'll see the manual for it). `bin/rails test` is supposed to "Runs all tests in test folder except system ones" and according to the docs http://guides.rubyonrails.org/command_line.html#bin-rails it looks like using `bin/rails test` is now the standard. – George Jun 29 '17 at 14:15