ROR unit test error: SyntaxError: (erb):18: - ruby-on-rails

I have a ROR project and all the unit tests are failing with the same error as listed below. To figure out the cause I created an empty test case, the code is follows.
require 'test_helper'
class UsersEditTest < ActionDispatch::IntegrationTest
test "the truth" do
assert true
end
end
When the run the test with "rake test TEST=test/integration/users_edit_test.rb" command I get the following error
1) Error:
UsersEditTest#test_the_truth:
SyntaxError: (erb):18: syntax error, unexpected keyword_end, expecting end-of-input
; end ; _erbout.force_encoding(__ENCODING__)
^
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Not sure what is missing in the project, I have other test projects where unit tests just fine.

It is probably not connected to tests, you have error in view (erb file).
Check if everything is ok in views, especially <%= tags

Related

Agile Web Development Rails 5 test failures

I just finished creating the Depot app from the Agile Web Development with Rails 5 textbook. For some reason, I am getting errors & failures when I run 'rails test'. Can someone explain why this is happening?
My Github repository is here: https://github.com/christopheragnus/Agile_Web_Development_with_Rails_5
Christophers-MacBook-Pro-3:depot Agnus$ rails test
Running via Spring preloader in process 45997
Run options: --seed 52040
# Running:
..............F
Failure:
LineItemsControllerTest#test_should_create_line_item [/Users/Agnus/Dropbox/CA/AgileWebDevelopment/depot/test/controllers/line_items_controller_test.rb:25]:
<Your Cart> expected but was
<>..
Expected 0 to be >= 1.
bin/rails test test/controllers/line_items_controller_test.rb:18
..E
Error:
OrderMailerTest#test_shipped:
NameError: undefined local variable or method `login_url' for #<OrderMailerTest:0x007ff7b60cc410>
Did you mean? login_as
test/test_helper.rb:12:in `login_as'
test/test_helper.rb:20:in `setup'
bin/rails test test/mailers/order_mailer_test.rb:12
E
Error:
OrderMailerTest#test_received:
NameError: undefined local variable or method `login_url' for #<OrderMailerTest:0x007ff7b6058880>
Did you mean? login_as
test/test_helper.rb:12:in `login_as'
test/test_helper.rb:20:in `setup'
bin/rails test test/mailers/order_mailer_test.rb:4
E
Error:
AdminControllerTest#test_should_get_index:
NameError: undefined local variable or method `admin_index_url' for #<AdminControllerTest:0x007ff7b4ca8880>
test/controllers/admin_controller_test.rb:5:in `block in <class:AdminControllerTest>'
bin/rails test test/controllers/admin_controller_test.rb:4
...........................
Finished in 5.740751s, 8.1871 runs/s, 14.4580 assertions/s.
47 runs, 83 assertions, 1 failures, 3 errors, 0 skips
The undefined errors mean just that: you are calling "login_url" but rails cannot find it anywhere. If you simply copy pasted code from the book you should go to the section where they create this helper file and see where they defined the login_url.
The test failure, opposed to the error, is that it is expecting an h2 element to exist with the name "Your Cart" but it cannot find it. To debug this, you could either go through the code and make sure all the routing/rerouting is correct and that the views are correctly structured and there's no typos- or run the app and go through what the test is doing and see if the results you are seeing is what is expected. Then- we can go from there.
As a rule of thumb, it is good to get into the habit of googling errors which can help give you an idea of where to troubleshoot the issues in your project.

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.

Ruby on Rails rspec runs tests succesfully, but Guard + Spork doesn't find methods for Devise

I tried to add a few tests to my Ruby on Rails -application. When I run them on command line using command rspec, it runs fine:
Finished in 0.87603 seconds 3 examples, 0 failures
When starting guard, it runs them fine for the first time as well. It gives this kind of message even though I'm not using minitest:
/.rvm/gems/ruby-2.0.0-p353/gems/minitest-4.7.5/lib/minitest/unit.rb:1037:in `block in process_args': invalid option: --drb (OptionParser::InvalidOption)
I haven't configured to use MiniTest, so I guess there might be something wrong with my configuration files?
However, when I make changes to my user_pages_spec.rb and save, which triggers Guard to run the tests, it gives this kind of error:
Running tests with args ["--drb", "-f", "progress", "-r", "/home/.rvm/gems/ruby-2.0.0-p353/gems/guard-rspec-2.5.0/lib/guard/rspec/formatter.rb", "-f", "Guard::RSpec::Formatter", "--failure-exit-code", "2", "spec/requests/user_pages_spec.rb"]...
Failures:
1)
User pages signup page
Failure/Error: before { visit new_user_registration_path }
ActionView::Template::Error:
undefined method `devise_error_messages!' for #<#<Class:0x00000006b18248>:0x00000006b226f8>
2)
User pages signup page
Failure/Error: before { visit new_user_registration_path }
ActionView::Template::Error:
undefined method `devise_error_messages!' for #<#<Class:0x00000006b18248>:0x000000066de560>
Basicly not finding those methods running the same tests the second time. Tests I run for another page which contains only static content works fine.
Here are some files:
Gemfile: http://pastebin.com/bHgxXYZ3
Guardfile: http://pastebin.com/Rnhz2tsJ
spec_helper: http://pastebin.com/Jpvu3k2Q
.rspec: http://pastebin.com/B0bzmE0n
If something's missing, just give me a note. Thanks for helping :)
Do you also have these errors when starting spork and then running your specs (without Guard)?
For the MiniTest error message, be sure that you don't have a test/ folder in your project.

Running tests for 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

Resources