How to view screenshot in terminal with rspec and rails 5.1 - ruby-on-rails

I am following this tutorial to test system specs with rails 5.1. If I deliberately fail a test, I get the following output in the terminal
WARN: Screenshot could not be saved. `page.current_path` is empty.
F
Failures:
1) Homepage shows greeting
Failure/Error: expect(page).to have_content 'Hello Worlds'
expected to find text "Hello Worlds" in "Hello World"
[Screenshot]: tmp/screenshots/failures_r_spec_example_groups_homepage_shows_greeting_506.png
# ./spec/spec/system/home_spec.rb:10:in `block (2 levels) in <top (required)>'
The file specified for the screenshot exists, but the screen shot is not shown in the terminal. I can open the screen shot manually, so it is being generated correctly.
If I type chromedriver --version I get
ChromeDriver 2.35.528157 (4429ca2590d6988c0745c24c8858745aaaec01ef)
and I am using version 63.0.3239.132 of Chrome.
How do I get the screenshots to open in terminal on Mac OSX?

Screenshot display depends on what kind of terminal you use and RAILS_SYSTEM_TESTING_SCREENSHOT environment variable.
For build-in terminal:
ENV['RAILS_SYSTEM_TESTING_SCREENSHOT'] = 'artifact'
For iTerm2:
ENV['RAILS_SYSTEM_TESTING_SCREENSHOT'] = 'inline'

Related

Could not open library 'lib.dylib' on Mac

Have some issues with launching ruby tests on Mac(Monterey 12.2.1). I've launched bundle install - everything was finished successfully, after that I tried to do a command bundle exec rake db:test:refresh, and got the following error
LoadError: Couldn't load the GEOS CAPI library.
/Users/alexandra_shimanovich/.bundle/ruby/2.5.0/gems/ffi-geos-0.3.0/lib/ffi-geos.rb:1000:in `rescue in <module:FFIGeos>'
/Users/alexandra_shimanovich/.bundle/ruby/2.5.0/gems/ffi-geos-0.3.0/lib/ffi-geos.rb:982:in `<module:FFIGeos>'
/Users/alexandra_shimanovich/.bundle/ruby/2.5.0/gems/ffi-geos-0.3.0/lib/ffi-geos.rb:51:in `<module:Geos>'
/Users/alexandra_shimanovich/.bundle/ruby/2.5.0/gems/ffi-geos-0.3.0/lib/ffi-geos.rb:7:in `<top (required)>'
/Users/alexandra_shimanovich/Documents/Curb/ride_manager/config/application.rb:9:in `<top (required)>'
/Users/alexandra_shimanovich/Documents/Curb/ride_manager/Rakefile:4:in `require'
/Users/alexandra_shimanovich/Documents/Curb/ride_manager/Rakefile:4:in `<top (required)>'
/Users/alexandra_shimanovich/.rbenv/versions/2.5.5/bin/bundle:23:in `load'
/Users/alexandra_shimanovich/.rbenv/versions/2.5.5/bin/bundle:23:in `<main>'
LoadError: Could not open library '/usr/lib/': dlopen(/usr/lib/, 0x0005): tried: '/usr/lib/' (not a file), '/usr/local/lib/' (not a file).
Could not open library 'lib.dylib': dlopen(lib.dylib, 0x0005): tried: 'lib.dylib' (no such file), '/usr/local/lib/lib.dylib' (no such file), '/usr/lib/lib.dylib' (no such file), '/Users/alexandra_shimanovich/Documents/Curb/ride_manager/lib.dylib' (no such file), '/usr/local/lib/lib.dylib' (no such file), '/usr/lib/lib.dylib' (no such file)
I looked inside ffi-geos.rb
def self.search_paths
#search_paths ||= begin
if ENV['GEOS_LIBRARY_PATH']
[ ENV['GEOS_LIBRARY_PATH'] ]
elsif FFI::Platform::IS_WINDOWS
ENV['PATH'].split(File::PATH_SEPARATOR)
else
[ '/usr/local/{lib64,lib}', '/opt/local/{lib64,lib}', '/usr/{lib64,lib}' ]
[ '/usr/local/{lib64,lib}', '/opt/local/{lib64,lib}', '/usr/{lib64,lib}', '/usr/lib/{x86_64,i386}-linux-gnu' ]
end
end
end
I know that in my computer lib files are not in /usr/local/.. but in /opt/homebrew/lib. But when I looked inside /opt/homebrew/lib I didn't find lib.dylib. There are only libgeos.3.10.2.dylib libgeos.dylib libgeos_c.1.16.0.dylib libgeos_c.1.dylib libgeos_c.dylib.
Please, help me find the right lib and somehow tell ffi-geos.rb to look in the right place, I would REALLY appreciate it! Thank you!
But when I looked inside /opt/homebrew/lib I didn't find lib.dylib
The code from ffi-geos.rb you posted is not looking for lib.dylib (or lib64.dylib), but it's adding /usr/local/lib, /usr/local/lib64, etc, to the search path to look for the specific GEOS dylibs. As your library files are in /opt/homebrew/lib, they won't be found by that automatic procedure.
The first conditional in the code shows you can set an environment variable to define the search path. So, set the library path with:
export GEOS_LIBRARY_PATH=/opt/homebrew/lib
Hope this works!

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.

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.

Cucumber not finding step definitions

My Cucumber just won't find the step definitions. The file structure (Only the specs folder inside the Rails root) looks like this:
-> specs
-> features
-> main_structure.feature
-> step_definitions
-> main_structure_steps.rb
This is the main_structure.feature:
Feature: Main structure
Scenario: Viewing the Structure page
When I am on the structure page
And this the main_structure_steps.rb:
When(/^I am on the structure page$/) do
visit '/'
end
Now I run the cucumber command like this:
→ cucumber spec/features -r features
I get this output:
Using the default profile...
Feature: Main structure
Scenario: Viewing the Structure page # spec/features/main_structure.feature:2
When I am on the structure page # spec/features/main_structure.feature:3
Undefined step: "I am on the structure page" (Cucumber::Undefined)
spec/features/main_structure.feature:3:in `When I am on the structure page'
1 scenario (1 undefined)
1 step (1 undefined)
0m0.229s
You can implement step definitions for undefined steps with these snippets:
When(/^I am on the structure page$/) do
pending # express the regexp above with the code you wish you had
end
/Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1037:in `block in process_args': invalid option: -r (OptionParser::InvalidOption)
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1016:in `new'
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1016:in `process_args'
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1066:in `_run'
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:1059:in `run'
from /Users/rudolf/.rvm/gems/ruby-2.0.0-p247#global/gems/minitest-4.7.5/lib/minitest/unit.rb:795:in `block in autorun'
There is also an error message at the bottom, that doesn't appear when I run the test in RubyMine. But in both cases, the step definitions are not found. This is the Rubymine output:
Testing started at 21:29 ...
You can implement step definitions for undefined steps with these snippets:
When(/^I am on the structure page$/) do
pending # express the regexp above with the code you wish you had
end
1 scenario (1 undefined)
1 step (1 undefined)
0m0.001s
Process finished with exit code 0
Tell me if you need any additional infos.
Try
cucumber spec/features/main_structure.feature -r spec/features
Similar to my question:Cucumber Test acting in a strange way
You can implement step definitions for undefined steps with these snippets:
When(/^I am on the structure page$/) do
pending # express the regexp above with the code you wish you had
end
As the error saying. Put the code above in web_steps.rb and then replace pending # express the regexp above with the code you wish you had with the code you want
I Also had a similar problem with Java and for me the reason was that I had 2 Cucumber plugins installed in NetBeans IDE. I left Cetriolo and removed Cucumber Features and tests started working
make sure glue syntax is like below:
glue={"packageName"}
Here packageName is the package where you step definition java file exists

Rspec test fails on Jenkins when open file from /tmp folder

I have such trouble with Jenkins.
I'm trying to test PDF generation using RSpec.
RSpec test failing, when trying to open receuntly generated PDF file. (File is saving into /tmp directory)
//TEST
pdf_generator = PdfGenerator::InvoicePdf.new(invoice)
pdf_file_path = pdf_generator.to_pdf
and I get error(in Jenkins only):
Failures:
1) OrdersController GET show while authorized as customer INVOICE .generate_pdf pdf contains correct text
Failure/Error: pdf_file_path = pdf_generator.to_pdf
Errno::ENOENT:
No such file or directory - /var/lib/jenkins/jobs/TicketCo for Gerrit/workspace/tmp/1371464314.pdf
# ./lib/pdf_generator/invoice_pdf.rb:80:in `to_pdf'
# ./spec/controllers/orders_controller_spec.rb:413:in `block (5 levels) in <top (required)>'
and here is the line in method, which causes error:
def to_pdf
Prawn::Document.generate(temp_pdf_file_path)
....
end
Can someone help me with this trouble ?
Maybe I need to change PDF path for Jenkins ?
I got an answer. If you have such problem just create /tmp folder in Jenkins workspace. In configurate tab you can add this command
mkdir -p tmp

Resources