Does RSpec require openstruct? - ruby

I have some strange behaviour in a small command line ruby app that parses cron strings.
I use an OpenStruct in part of my code and I forgot to require "ostruct". My tests ran green and the code using OpenStruct is well tested.
However when I attempted to run the application 'as a user' from the command line I receive a uninitialized constant CronParser::OpenStruct error. I have added require "ostruct" and all is well again.
RSpec is bringing in OpenStruct and so my tests are not failing. But the application does not work. Clearly not ideal.
I would appreciate any thoughts on how I should be testing for this?
Thanks in advance.
T

Related

humanize NoMethodError on padrino-0.14.1.1

I used padrino-0.14.1.1 and activesupport-5.1.1 in combination and I generate padrino admin app. When I display the login page (/admin/sessions/new) the following error occurred and become 500 error page.
DEBUG - TEMPLATE (0.0007s) /sessions/new
2017-11-07 20:23:01 - NoMethodError - undefined method `humanize' for "login.title":String:
/Path/to/app/'vendor/bundle' /ruby/2.3.0/gems/padrino-admin-0.14.1.1/lib/padrino-admin/helpers/view_helpers.rb:43:in `padrino_admin_translate'
...
I think it is not a bug of Padrino, because I cannot find same problems in the Internet. And I add the following require to the beginning of view_helpers.rb provisionaly.
require 'active_support'
require 'active_support/core_ext/string'
However, I think it is not good to edit Padrino's files because of my application problem. Please let me know if you have any other good countermeasures.
I got the answer of this question from Padrino maintainer on Github.
And I tried the suggestion in the answer, that worked well.
The answer is following.
--
Looks like a bug due to the ongoing effort to remove the ActiveSupport dependency from Padrino.
You don't need to update view_helpers.rb - creating a file in config/initializers should be sufficient as a temporary stop-gap until the bug is fixed.
# config/initializers/extra_requires.rb
require "active_support"
require "active_support/core_ext/string"

I am getting an RPC error message when running my cucumber tests against a password protected https url

So what am I using:
I am using the following gems on Ruby 1.9.3:
capybara, commander, cucumber, cucumber-rails, fakeweb, factory_girl_rails, flexmock, gherkin, parallel, parallel_tests, poltergeist, rspec, rspec-rails, sauce, sauce-connect, sauce-cucumber, selenium-webdriver'
For my config file I am using yaml. so config.yml
Now to access the homepage of the site I am testing in my config I have
base_url: https://<username>:<password>#the.url.com
When running my cucumber tests (using poltergeist) the following message is shown several times while the tests are running:
Invalid rpc message origin. https://username#the.url.com vs https://the.url.com
It does not cause the tests to fail but is incredibly untidy and I would really like to get rid of it.
I have been, and still am, investigating a solution to this but if someone gets there first that would be amazing.
Some things that I have tried/know what is happening:-
I know my tests are working as I have run them using the browser (firefox) and it is fine with none of these messages.
Also if I remove the s from https the message dissapears. But alas that will not work for the site as require https.
Putting the url in double quotes does not solve the problem.
I have pinpointed the issue directly to the config and specifying the UN and PW in the url.
It looks like your username and password have gone AWOL:
Invalid rpc message origin. https://#the.url.com vs https://the.url.com
The first part should be https://username:password#the.url.com. Could it be because your config is lacking the double slashes?
base_url: https:<username>:<password>#the.url.com
should probably be
base_url: https://<username>:<password>#the.url.com

capybara-webkit: automatically save a screenshot on an RSpec test failure

How can I automatically save the html and a screenshot when a test fails using capybara-webkit with Rspec? How can I execute a callback when an RSpec test fails.
Bonus points: how can I avoid getting the following error:
Capybara::Driver::Webkit::WebkitInvalidResponseError
when executing this code:
require 'capybara/util/save_and_open_page'
path = "/#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}"
png = Capybara.save_and_open_page_path + "#{path}.png"
page.driver.render Rails.root.join(png)
I have written a gem Capybara-Screenshot specifically for this, check out https://github.com/mattheworiordan/capybara-screenshot
It will automatically create screen shots on failed RSpec or Cucumber steps.
Capybara provides a function for saving and opening a screenshoot during the testing. You just need to call anywhere in your test:
save_and_open_screenshot
and it will open a picture how the test looks like at that point.
No need for any additional gems.
Capybara::save_and_open_screenshot
Found a gist that might help you https://gist.github.com/1156691

MongoMapper in a custom gem with rspec

Hopefully I'm just missing something simple. I am using mongomapper in a rails 3 app and it works beautifully. I needed to share it with another app and pushed it out to a gem, which also works great from both rails apps. However, when I try to add rspec tests to the gem I am getting the following error:
/Users/dane/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.2/lib/rspec/core/backward_compatibility.rb:20:in `const_missing': uninitialized constant User::MongoMapper (NameError)
I am adding a really simple test for my User model.
Thanks!

mysql timeout error in rails 3 when using delete_all and make! in step definition

I'm using following construct in a cucumber step definition.
Given "I have following stuff" do
Model.delete_all
list.each { |i| Model.make!(:name => i) }
end
(make! is from machinist 2).
Above step fails with INSERT statement timeout. When I open up a console for test, environment, I can execute each statement without an issue. Also, if I disable transactional features timeout goes away.
Can anyone please help me fix this? (This ran without an issue with rails 2.x)
(Database MySQL)
This turned out be an issue with machinist beta version I was using. I'm not 100% sure what was wrong, but when I used pure ActiveRecord code to create the objects, instead of make! it worked.
Then I switched to Factory_Girl instead of machinist, and now it works fine.

Resources