Ruby on Rails Testing - ruby-on-rails

I added a controller action using the Rails testing technique, however despite solving all the errors of the new controller action created, a new error regarding the already existing controller actions appeared. Could anyone have an idea of how to go about it? This is the error I'm receiving on my terminal:
Error:
StaticPagesControllerTest#test_should_get_help:
ActionView::Template::Error: Permission denied # rb_file_s_rename - (C:/Users/ADMIN/Desktop/sample_app/tmp/cache/assets/sprockets/v4.0.0/Ah/AhRKfpIltIZZsQQ3DW_RRap2qgrZpmLfGMuTS7wKLvE.cache.16460.9436.19047, C:/Users/ADMIN/Desktop/sample_app/tmp/cache/assets/sprockets/v4.0.0/Ah/AhRKfpIltIZZsQQ3DW_RRap2qgrZpmLfGMuTS7wKLvE.cache)
app/views/layouts/application.html.erb:9
test/controllers/static_pages_controller_test.rb:10:in `block in <class:StaticPagesControllerTest>'

The solution was I changed this code in the test_helper.rb from:
parallelize(workers: :number_of_processors, with: :threads)
To:
parallelize(workers: 1)
Its works!

Related

Rails test parallelization not working with custom PG text search config

When I use parallelize(workers: :number_of_processors) for my tests this error occurs :
DRb::DRbRemoteError: PG::UndefinedObject: ERROR: text search configuration "french_custom" does not exist
LINE 1: ...earch" tsvector GENERATED ALWAYS AS ((to_tsvector('french_custom...
I tried to force tsearch config on database fork with parallelize_setup, but it's not even called

Rspec uninitialized constant for class nested inside module - Ruby on Rails

In a Rails project, I have created a new class inside the lib directory, this class is namespaced inside a module. When creating a spec file for it, I'm seeing NameError: uninitialized constant MyNamespace.
Here is my folder structure
app/
...
lib
my_namespace
my_new_class.rb
another_namespace
another_old_class.rb
spec
lib
my_namespace
my_new_class_spec.rb
another_namespace
another_old_class_spec.rb
Here the (abbreviated) contents of:
lib/my_namespace/my_new_class.rb
module MyNamespace
class MyNewClass
end
end
spec/lib/my_namespace/my_new_class_spec.rb
RSpec.describe MyNamespace::MyNewClass do
it "is true"
expect(true).to eq(true) # irrelevant at this point
end
end
The reason I included another_old_class_spec.rb is that its tests run without issue and I can't find anywhere that it's explicitly loaded or required in the test setup (in case that might be a potential issue).
When running the test with bundle exec rspec spec/lib/my_namespace/my_new_class_spec.rb or even bundle exec rspec spec/lib/my_namespace/ I get
An error occurred while loading ./spec/lib/my_namespace/my_new_class_spec.rb
Failure/Error:
RSpec.describe MyNamespace::MyNewClass do
NameError:
uninitialized constant MyNamespace
Like Georgiy Melnikov implied in his comment, by default the /lib directory is not in autoload paths, so the constant MyNamespace is not automatically resolved.
You basically have two options to fix this:
explicitly require the file with require lib/my_namespace/my_new_class at the top of the spec file
add lib/ to autoload paths (nowadays this is discouraged)

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.

Rspec spec failing -- undefined method `permissions' for RSpec::ExampleGroups::UserPolicy:Class (NoMethodError)

I'm installing an app with Pundit authorization and when I try to run RSpec tests I get:
undefined method `permissions'
for RSpec::ExampleGroups::UserPolicy:Class
(NoMethodError)
Also remember to add this your rails_helper
require 'pundit/rspec'
Found out what the problem was...
Following line in rails_helper.rb was commented out:
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Activating it made the tests work correctly :)
I had require 'pundit/rspec' but I had misspelt "policies" in the spec directory structure:
spec/polices/my_policy_spec.rb
My mistake also returned the permission error message.
You can fix it by spelling policies correctly, alternatively, set the type of the test file to policy.
RSpec.describe MyPolicy, type: :policy do
...
...
end

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.

Resources