Railsapp OAuth Tutorial, rspec "unitialized constant" error - ruby-on-rails

Following the tutorial, I created a file omniauth.rb at the path
spec/support/helpers/omniauth.rb
module Omniauth
module Mock
def auth_mock
OmniAuth.config.mock_auth[:twitter] = {
'provider' => 'twitter',
'uid' => '123545',
'user_info' => {
'name' => 'mockuser'
},
'credentials' => {
'token' => 'mock_token',
'secret' => 'mock_secret'
}
}
end
end
end
But when I run rspec, I get an error with "uninitialized constant Omniauth"
rails-omniauth/spec/support/helpers.rb:2:in `block in <top (required)>': uninitialized constant Omniauth (NameError)
It seems clear that either omniauth.rb or helpers.rb should be in a different location, but I don't know where.
Update:
I subsequently tried installing the rails-omniauth via the Rails Composer app. When I run "rspec" for this app, I get exactly the same error.

At one point in the tutorial you are given a choice between creating a file at at /spec/support/helpers.rb:
RSpec.configure do |config|
config.include Omniauth::Mock
config.include Omniauth::SessionHelpers, type: :feature
end
OmniAuth.config.test_mode = true
Or adding these same lines to /spec/rails_helper.rb.
I created the new file at /spec/support/helpers.rb. To make this work, I needed to add the line require_relative 'helpers/omniauth' at the top of the file. The Rails Composer app also adds the helpers.rb file rather than editing rails_helper.rb, so the same line is needed to make rspec run successfully for that app.

Related

uninitialized constant (Name error) with rails-rspec

i have created a spec/support/features directory. Added a file named sign_in.rb
with a module
module Features
def sign_in
end
end
but when i added this to spec/rails_helper.rb file as
....
config.include Features, type: :feature
end
then while running rake its showing uninitialized constant Features (Name error). I really did not understand this. Asking for help.
all i needed to add following line to rails_helper.rb
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

How To Use Spree Route Helpers in RSpec

I am trying to use the Spree 2.3 route helpers in Rspec 3.0. In the main app, I can access them by prefixing spree., like so:
spree.admin_login_path => 'spree/admin/user_sessions#new'
However I can't seem to access them in Rspec.
#rspec error
undefined local variable or method `spree_admin_login_path'
I've found reference to including the helpers in the rails_helper file, but this throws an error
# app/spec/rails_helper.rb
RSpec.configure do |config|
config.include Spree::Core::UrlHelpers
end
# configuring the above results in the following
app/spec/rails_helper.rb:21:in `block in <top (required)>': uninitialized constant Spree (NameError)
How do I access the spree routes given in $ rake routes in my tests?
After digging through the Spree code I was able to put together this setup for my rails_helper file that lets me use spree routes such as spree.admin_login_path in my spec files:
# app/spec/rails_helper.rb
require 'spree/testing_support/url_helpers'
RSpec.configure do |config|
config.include Spree::TestingSupport::UrlHelpers
end
I'm sure there's a smoother way to include all of Spree's test helpers, and I'd love to hear about it from someone who knows.

Using ActionView::TestCase::Behavior and the view method in a presenter spec

Using the Railscast example, I have written a spec for my presenter which includes ActionView::TestCase::Behavior and passes in the view method to the presenter.
spec/spec_helper.rb:
...
config.include ActionView::TestCase::Behavior, :example_group => {:file_path => %r{spec/presenters}}
...
spec/presenters/order_presenter_spec.rb:
require 'spec_helper'
describe OrderPresenter do
describe "#subtotal" do
subject { OrderPresenter.new(order, view).subtotal }
let(:order) { stub(:order, working_subtotal: 4500) }
it "renders the subtotal table row" do
should == "<tr><th>SUBTOTAL</th><td>$45.00</td></tr>"
end
end
end
However, this gave me two errors.
The first was
/Users/shevaun/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:12:in `block in <module:TemplateAssertions>': undefined method `setup' for #<Class:0x007fe2343b2f40> (NoMethodError)
so I included ActiveSupport::Testing::SetupAndTeardown in the same way as ActionView::TestCase::Behavior.
Fixing that gave me the error:
NoMethodError:
undefined method `view_context' for nil:NilClass
when calling view. This is caused by the #controller instance variable inside ActionView::TestCase being nil.
I am using Rails 3.2.13 and rspec-rails 2.13.0 and have another application using the same versions which just works.
The only thing I can think of that might make a difference is that this app is using MongoDB so maybe the ActiveRecord application includes something which sets up #controller for free?
I have got a workaround which makes the presenter specs pass, but I would like to know how #controller normally gets instantiated, and if there's a more elegant way to do this for a MongoDB project (if it is ActiveRecord that's doing the magic).
My current solution is to instantiate the #controller instance variable by calling setup_with_controller before the presenter specs.
spec_helper.rb:
RSpec.configure do |config|
config.include ActiveSupport::Testing::SetupAndTeardown, :example_group => {:file_path => %r{spec/presenters}}
config.include ActionView::TestCase::Behavior, :example_group => {:file_path => %r{spec/presenters}}
config.before(:each, example_group: {:file_path => %r{spec/presenters}}) do
setup_with_controller # this is necessary because otherwise #controller is nil, but why?
end
...
end
You can also create your own view:
let(:view) { ActionController::Base.new.view_context }
subject { OrderPresenter.new(order, view).subtotal }
https://www.ruby-forum.com/topic/2922913#1029887

Trying to test omniauth with rspec & Capybara, failing

Using Rails 3.2 and the latest Rspec and Capybara, which means my Capybara specs live in spec/features.
I'm really new to Rails and testing, but I want to get used to testing. I ended up implementing OAuth before testing it. I finally got it working, and now I'm trying to retroactively test it (so I at least know if it breaks in the future). I'm trying to follow this tutorial, but things aren't working. Here's what I did:
1) Created spec/support/integration_spec_helper.rb with:
module IntegrationSpecHelper
def login_with_oauth(service = :google)
visit "/auth/#{service}"
end
end
2) Modified spec/spec_helper to include config.include IntegrationSpecHelper, :type => :request inside the Rspec.configure do block.
3) Created spec/features/omniauth_spec.rb with:
require 'spec_helper'
feature 'testing oauth' do
scenario 'should create a new tiger' do
login_with_oauth
visit new_tiger_path
fill_in 'tiger_name', :with => 'Charlie'
fill_in 'tiger_blood', :with => 'yes'
click_on 'Create Tiger'
page.should have_content("Thanks! You are a winner!")
end
end
Of course it's going to fail (I don't have tigers in my app) but I want it to fail on visit new_tiger_path. Instead, running the spec, I get:
1) testing oauth should create a new tiger
Failure/Error: login_with_oauth
NameError:
undefined local variable or method `login_with_oauth' for #<RSpec::Core::ExampleGroup::Nested_3:0x83355d8>
# ./spec/features/omniauth_spec.rb:4:in `block (2 levels) in <top (required)>'
So basically, it says there's no such thing login_with_oauth. This must be a really basic error, as my code isn't included for some reason.
I'm not using spork (trying to keep things simple).
Any idea what the problem might be? Thanks in advance!
If you are trying to use oauth from google, you'll want to change:
def login_with_oauth(service = :google)
to:
def login_with_oauth(service = :google_oauth2)
:google_oauth2 should also be the first argument to OmniAuth.config.add_mock, i.e.:
OmniAuth.config.add_mock(
:google_oauth2,
{
:info => {
:email => 'test#some_test_domain.com',
:name=>'Test User'
}
})
Don't forget to change:
config.include(IntegrationSpecHelper, :type => :request)
to:
config.include(IntegrationSpecHelper, :type => :feature)
inside the RSpec.configure block, as Christoph noted above.
A little late, but maybe I can help.
Got the same problem. It's caused by
config.include IntegrationSpecHelper, :type => :request
The paramater ':type' needs to be changed to ':feature' because you write a rspec feature test.
Solution:
config.include IntegrationSpecHelper, :type => :feature
Unfortunately this causes further problems, I couldn't solve yet.
Regards,
C-

Factory_Girl rake issue

I'm creating a new Rails 3.1 application using Cucumber, Devise, and Factory_Girl. I installed successfully Devise and Cucumber. I then created the spec/factories.rb file that contains the following:
require 'factory_girl'
Factory.define :user do |user|
user.name 'Test User'
user.email 'test#email.com'
user.password' testpass'
end
I also added spec/support/devise.rb to add in the test hooks for Devise:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
In regards to testing, I have not touched anything else and now everytime I run any rake command I get the following (I ran rake spec in this case, just to make sure everything is ready for tests):
rake aborted!
Factory already registered: user
Tasks: TOP => spec => db:test:prepare => db:abort_if_pending_migrations => environment
(See full trace by running task with --trace)
Now if I remove the Factory.define block from the factories file, it runs fine. I've done some Google searching and have come up with absolutely nothing. Since it's saying the Factory is already registered, is Devise for whatever reason, creating a factory already?

Resources