I've created my own service (which is Plain Old Ruby Object) in app/services/nested/auth_service.rb. Of course I decided to write tests for it, so I created file in RSpec under spec/services/nested/auth_service_spec.rb and wrote inside:
require 'spec_helper'
RSpec.describe Nested::Auth do
end
I added spec_helper because Nested::Auth is just PORO without any rails stuff inside (so rails_helper would be overkill here, right?)
Unfortunately while running rspec command I got error: uninitialized constant Nested::Auth
I added $: << '../app/services' at the top of spec_helper.rb but didn't help. $: << '../services' also didn't work.
How to let spec_helper see /services directory? How to fix uninitialized constant error?
Just follow Rails convention and name your services with Service postfix.
This way Nested::AuthService. Works perfectly now.
In the pasted error message, the constant is misspelled "uninitialized constant Nasted::Auth" where "Nasted" has an "a" instead of an "e". Check any of your files for the misspelling and fix it if you find anything.
Related
So I've been messing with this for the last hour and a half or so and just can not figure out how to do this.
In my Ruby test.rake file, which gets called from another project after a Snapshot Gem is created, I need to check to see if a dependency for Capybara exists in the Rakefile for the other project, since we have since deprecated Capybara and are replacing it with something else.
So, I included a simple check to see if the project contains Feature/Capybara tests, and if so, to scan through each spec test and see if it has dependency for Capybara in it, and if so log a warning to tell the user that Capybara has been deprecated.
My problem is, I can not get this to work at all. When I attach this to a Jenkins job and run it, I keep getting the unexpected Print statement in the else clause.
Am I missing something here? From what I understand, my code is doing the following things:
Checking if the spec/features directory exists
Using glob to select all of the files in the directory that end with .rb (Spec tests), with each file instance being assigned to file
Take the current instance of file, read each line of it, and use grep to find any instances of the word "capybara" in them, and print out the right message if found.
Else, if the word is NOT found, print the error message in the else clause.
I'm somewhat new to Ruby so I feel like maybe I am missing something with the syntax, but other than that I'm stumped! Any help would be greatly appreciated! Here is what the code and the Rakefile I am trying to parse through look like:
test.rake:
if Dir.exist?('spec/features')
path = 'spec/features/'
Dir.glob(path + '*.rb') {|file|
if File.readlines(file).grep(/capybara/).any?
print "The capybara dependency has been found!"
else
print "SOMETHING WENT WRONG"
end
}
end
Rakefile:
#!/usr/bin/env rake
# frozen_string_literal: true
require 'roll_out/rake_tasks'
require 'roll_out/security'
require 'roll_out/jira'
require 'REDACTED/philter/lint_results'
require 'roll_out/capybara'
load 'REDACTED/tasks/REDACTED.rake'
Your code looks almost good except .each missing
i did my test and it works fine.
Dir.glob('spec/features/*.rb').each do |f|
puts 'haha' if File.readlines(f).any?{|line| line.include?('capybara')}
end
I've got a class inside app/models/parser called data.rb with contents :
class Parser::Data
def method1
end
end
Nothing fancy at this point. I'm trying to write a test for it before implementing too much, just did default RSpec install for Rails.
My RSpec file is in spec/models/parser/data_spec.rb and is very basic so far:
require 'spec_helper.rb'
describe Parser::Data do
let(:parser) { Parser::Data.new }
end
When I run the test I get this error:
spec/models/parser/data_spec.rb:3:in `<top (required)>': uninitialized constant Parser (NameError)
I tried placing module Parser around the Data class in the same directory app/models/parser, also I've tried moving it to lib/parser doing the same module wrapping class, and added lib/parser to autoload in the application.rb but nothing has worked so far.
What am I doing wrong?
require 'rails_helper' instead of spec_helper. Requiring only spec_helper reproduces the issue for me, and requiring rails_helper fixes it. More on spec_helper vs. rails_helper (including performance implications) here: How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?
I reproduced the problem by running RSpec with bundle exec rspec. If I run RSpec with bin/rspec (that's a binstub generated by the spring-commands-rspec gem) it doesn't care which helper file I require. I guess spring loads more eagerly.
I'm running a basic feature by following RBates RailsCasts tutorial using Rspec 2.5.0 and Cucumber-rails 0.4.1 on a cygwin environment. I am at the step where I am testing "Then I should see"
For example:
Scenario: Stores List
Given I have stores named Pizza, Breadsticks
When I go to the list of stores
**Then I should see "Pizza"**
Running cucumber features gives me the following error message:
Undefined local variable or method 'page' for Cucumber::Rails::World (NameError)
Then I should See is defined in the web_steps file as follows:
if page.respond_to? :should
page.should have_content(text)
else
assert page.has_content?(text)
end
Any guidance would be appreciated!
Thank you!
Fixed the error. I had commented out:
Capybara.default_selector = :css because of a previous issue (See: https://github.com/aslakhellesoy/cucumber-rails/issues/120). Once i included the following:
require 'capybara/rails'
require 'capybara/cucumber'
It fixed the capybara issue and the page method was available.
Thanks.
I don't know much of RoR, and without seeing more code, it appears you haven't defined the variable 'page', or you defined it in an area that is outside the scope of where you're trying to use it.
I had a working (and working well) ImageScience install, that did some simple resizing to various dimensions of images, and then copying them to different directories. All very simple. This small and simple routine was in a rake task. Upon update to Rails 3, this rake task will still work (it does some AR inserts and audio encoding as well), but the image_science require fails with a message like this,
"require on /home//.ruby_inline/Inline_ImageScience_cdab.so failed"
I've ruled out a duff ImageScience install, as I can go into IRB and do some simple calls to ImageScience and make thumbnails. The remainder of the rake task works as well as it did before if I comment out any mention of requiring 'image_science' or the ImageScience routine.
the output from rake on failure is this,
/var/lib/gems/1.8/gems/RubyInline-3.8.6/lib/inline.rb:513:in `load'
/var/lib/gems/1.8/gems/RubyInline-3.8.6/lib/inline.rb:829:in `inline'
/var/lib/gems/1.8/gems/image_science-1.2.1/lib/image_science.rb:90
...
<active_support complaints >
...
/home/<user>/RailsApps/marlow/lib/tasks/flac_import.rake:2
...
<rails complaints>
...
/home/<user>/RailsApps/marlow/Rakefile:7
...
<standard complaints to end>
the Rakefile in the rails app root is a stock and standard Rails 3 Rakefile, like this,
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
require 'rake'
Marlow::Application.load_tasks
the last line is line 7.
I'm kind of stumped as to what's breaking it, and Google doesn't seem to shed anything. Does anyone know why RubyInline is complaining? Or why this once working Rake task is suddenly unhappy how ImageScience is being called? OS is Ubuntu 10.10, but it was all working prior to the Rails 3 upgrade.
Thanks in advance
This does seem to be the problem, but there is a simpler fix I found from perusing the comments at carlhuda issues 431
I had the same problem and it worked for me. Simply change the require method to be Kernel.require.
After that there's no need to pepper your code with require image_science statements.
There is a fix, but you'll need to jump through few hoops.
First delay image_science load:
gem 'image_science', :require => false
Then monkey patch ruby-inline (which image_science relies on). Place this code in config/initializers/ruby_inline_hack.rb:
class Inline::C
def load
require "#{so_name}"
#below is the original version which breaks
#require "#{so_name}" or raise LoadError, "require on #{so_name} failed"
end
end
Then require 'image_science' wherever you're using it. And voila.
One note on aremave's answer:
It looks like the original code has a bug! It's not using short-cut-evaluation!
class Inline::C
def load
require "#{so_name}" || raise LoadError, "require on #{so_name} failed"
end
end
Notice the || , which will stop the evaluation of the logical expression if the first part is true.
If there is an 'or' in the same place, the second part of the expression will always be evaluated,
hence the error you're seeing...
as seen on bundler issue tracker, it worked for me.
Point your gem file to https://github.com/asynchrony/image_science We rebuilt image science without ruby inline.
In order to override the table_exists? method in the Rails PostgreSQL adapter I have tried the following in an initializer file:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
def table_exists?(name)
raise 'got here'
end
end
This will raise the the following error:
uninitialized constant ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
I believe this would have worked in previous versions of rails and I even found a small plugin that did something like this in Rails 2.3.6. Also I only encounter this error when I am trying to run a rake task like db:migrate and not when I start my application server.
Could someone show me the right way to do this and / or explain why PostgreSQLAdapter doesn't seem to be loaded when I am in an initializer file?
Instead of config/initializers, place that code in lib/ folder.
While this means that the active_record is loaded after the rails initializers, which is unusual. I ll update this with more detail, once I am done investigating the whole flow. If you want some more details about the rails 3 initialization process, check out this link:
http://ryanbigg.com/guides/initialization.html
I had success by moving this code into a Rails plugin. It is a little bit more overhead, but it is working consistently when I run rails s and when I run rake db:migrate.
I just followed the rails guide page on the topic and ran
rails generate plugin rails_patches --with-generator
and moved my init.rb file into rails as recommended.
~vendor/
`~plugins/
`~rails_patches/
|~lib/
| `-rails_patches.rb
|~rails/
| `-init.rb
|+test/
|-install.rb
|-MIT-LICENSE
|-Rakefile
|-README
`-uninstall.rb
I put this code in init.rb:
require 'rails_patches'
I put this code in rails_patches.rb:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
def table_exists?(name)
raise 'got here'
end
end
This now behaves as I expected.