Include helper not works - ruby-on-rails

I'm trying to include some helpers to my test but I can't make that it works.
I got the following error:
/home/edu/.rvm/rubies/ruby-1.9.3-p392/bin/ruby -S rspec ./spec/features/customers_spec.rb ./spec/features/login_spec.rb ./spec/features/products_spec.rb ./spec/features/suppliers_spec.rb
/home/edu/Desktop/rails_proyects/gg/spec/support/features.rb:2:in `block in <top (required)>': uninitialized constant MyHelp (NameError)
from /home/edu/.rvm/gems/ruby-1.9.3-p392#gg/gems/rspec-core-2.14.6/lib/rspec/core.rb:120:in `configure'
from /home/edu/Desktop/rails_proyects/gg/spec/support/features.rb:1:in `<top (required)>'
I have this:
# spec/support/features/session_helper.rb
module MyHelp
module SessionHelpers
...
def sign_in
...
end
end
end
# spec/support/features.rb
RSpec.configure do |config|
config.include MyHelp::SessionHelpers, type: :feature
end
I'm using it here:
# spec/features/login_spec.rb
require 'spec_helper'
feature "Login" do
scenario "with valid credentials" do
user = create(:user)
sign_in user.email, user.password
page.should have_content(I18n.t('layouts.header.exit', locale: 'es'))
end
end
I'm using:
rspec (2.14.1)
rspec-core (2.14.6, 2.14.5)
rspec-expectations (2.14.3, 2.14.2)
rspec-mocks (2.14.4, 2.14.3)
rspec-rails (2.14.0)
ruby 1.9.3p392
rails 3.2.13
Can someone help me with this?
thank you.

It looks like you just need to require the new helper before you try to use it in spec/support/features.rb
require Rails.root.join('spec/support/features/session_helper')
Also, it's best practice to have your class/module match the file name, so either the file should be pluralized, or the helper singularized.

Related

How to login in feature specs

I have a helper method that is using #request.env and Devise to login the user:
def login_user(user)
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in user
end
I'm trying to write a feature spec where I need to login, but login_user is failing:
1) Search finds a manufacturer
Failure/Error: #request.env["devise.mapping"] = Devise.mappings[:user]
NoMethodError:
undefined method `env' for nil:NilClass
# ./spec/support/controller_macros.rb:3:in `login_user'
# ./spec/features/search_spec.rb:17:in `block (2 levels) in <top (required)>'
How can I fix? I have no experience with feature specs, with cucumber I'd use a feature to login, I'm definitely not sure that's the best practice with rspecs.
Thanks in advance.
In Capybara feature specs in my app, we use the Warden test helpers:
# spec/rails_helper.rb
RSpec.configure do |config|
config.include Warden::Test::Helpers
Warden.test_mode!
end
# in the feature spec
login_as(user, scope: :user)
Also, for controller specs:
allow(controller).to receive(:current_user).and_return(user)
I was working with different library then Devise but it should works. It's very simple mock:
allow_any_instance_of(ApplicationController).to receive(:current_user) { user }
You can make it even simpler by making a special helper:
# spec/support/feature_spec_helper.rb`
module FeatureSpecHelper
def login(user)
allow_any_instance_of(ApplicationController).to receive(:current_user) { user }
end
end
Then in spec config (spec_helper or rails_helper) drop anywhere
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# (...)
RSpec.configure do |config|
config.include FeatureSpecHelper, :type => :feature
# (...)
next you can use in feature spec login user
You can use Capybara as well to connect your test user. In my rails_helper I have this method:
def create_user_and_log_in
create :user, email: 'user#test.com', password: 'password'
visit new_user_session_path
fill_in :user_email, with: 'user#test.com'
fill_in :user_password, with: 'password'
click_on 'Connexion'
end
You can then call this method in your specs.

I was trying write permissions specs for CanCan by RyanBates but got error "uninitialized constant Ability::CanCan"

I dont think this issue CanCan uninitialized constant Ability::CanCan addresses my problem.
According to CanCan Its not hard to test the Abilities . I followed https://github.com/ryanb/cancan/wiki/Testing-Abilities to write specs:
When I try the following command
bundle exec rspec spec/cancan/ability.rb
I get the following error
/projects/ATS/app/models/ability.rb:2:in `<class:Ability>': uninitialized constant Ability::CanCan (NameError)
from /home/shiva/projects/ATS/app/models/ability.rb:1:in `<top (required)>'
from /home/shiva/projects/ATS/spec/cancan/ability.rb:3:in `<top (required)>'
from /home/shiva/.rvm/gems/ruby-2.1.1/gems/cancan-1.6.10/lib/cancan.rb:1:in `<top (required)>'
from /home/shiva/projects/ATS/spec/rails_helper.rb:14:in `<top (required)>'
from /home/shiva/projects/ATS/spec/cancan/ability.rb:1:in `<top (required)>'
My code in spec/cancan/ability.rb
require 'rails_helper'
RSpec.describe Ability, type: :model do
subject(:ability) {Ability.new(user)}
let(:user) {nil}
context 'Logged in as CSA' do
let(:org) {create(:organization)}
let(:user) {create(:user, organization: org, role_ids: [Role::ROLE_CSA])}
describe 'Not Permitted' do
it 'to visit /admins' do
it {is_expected.to be_able_to(:manage, Workflow.new)}
end
end
end
end
and code in snippet in models/ability.rb
class Ability
include CanCan::Ability
def initialize(user, url, admin_session_id, my_applicant_id)
if user.present?
user.roles[0].permissions_by_organization(user).each do |permission|
case permission.subject_class
Version Details of components
Rails 4.0.4
rspec-core (3.1.7)
rspec-expectations (3.1.2)
rspec-mocks (3.1.3)
rspec-rails (3.1.0)
rspec-support (3.1.2)
cancan (1.6.10)
I got the answer for my question.
The issue was the filename of the spec file
It should have been
spec/cancan/ability_spec.rb but it was
spec/cancan/ability.rb
and I should have invoked
bundle exec rspec spec/cancan/ability_spec.rb
For more details please visit this link RSpec naming conventions for files and directory structure

rspec-rails and factory girl block in <top (required)>': undefined method `build'

I have a new Rails 4 project with FactoryGirl and rSpec. In my spec_helper.rb I have:
# lots of stuff
RSpec.configure do |config|
# more stuff
config.include FactoryGirl::Syntax::Methods
end
I also removed the rspec/autorun require in this file.
A simple spec:
require 'spec_helper'
describe User do
build(:user)
end
with a simple factory:
FactoryGirl.define do
factory :user do
email "somename#someplace.com"
end
end
Fails with the following message.
`block in <top (required)>': undefined method `build' for #<Class:0x007fd46d0e3848> (NoMethodError)
However, if I explicitly qualify build in the spec like this it passes:
require 'spec_helper'
describe User do
FactoryGirl.build(:user)
end
What can I do so I don't have to add FactoryGirl every time?
The methods passed to config.include are only included by RSpec within the it, let, before and after blocks, not at the top level of describe. Since that's where you generally need to put your setup and test logic, in practice it's not really an issue.

`Zlib::GzipFile::Error` when using `vcr` with rspec

I'm doing Railscast #291 Testing with VCR (Pro).
I want to use rspec with vcr. The tests without vcr pass with this code.
# spec/requests/zip_code_lookup_spec.rb
require "spec_helper"
describe "ZipCodeLookup" do
it "show Beverly Hills given 90210" do
visit root_path
fill_in "zip_code", with: "90210"
click_on "Lookup"
page.should have_content("Beverly Hills")
end
end
And as the tutorial I've put code in a VCR.use_cassette like this:
require "spec_helper"
describe "ZipCodeLookup" do
it "show Beverly Hills given 90210" do
VCR.use_cassette "zip_code/90210" do
visit root_path
fill_in "zip_code", with: "90210"
click_on "Lookup"
page.should have_content("Beverly Hills")
end
end
end
And created this file:
# spec/support/vcr.rb
VCR.configure do |c|
c.cassette_library_dir = Rails.root.join("spec", "vcr")
c.stub_with :fakeweb
end
According to the tutorial the rspec test should pass with this, but it fails with this error:
1) ZipCodeLookup show Beverly Hills given 90210
Failure/Error: click_on "Lookup"
Zlib::GzipFile::Error:
not in gzip format
# ./app/models/zip_code.rb:6:in `initialize'
# ./app/controllers/zip_code_lookup_controller.rb:3:in `new'
# ./app/controllers/zip_code_lookup_controller.rb:3:in `index'
# ./spec/requests/zip_code_lookup_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/requests/zip_code_lookup_spec.rb:5:in `block (2 levels) in <top (required)>'
I have no idea why gzip appears here and not in the rest of the rails project.
How can I solve this problem?
Just had the same issue: the fakeweb gem is now deprecated in VCR, use webmock instead
in your Gemfile, replace
gem 'fakeweb'
by
gem 'webmock'
re-bundle, and you should be sorted

Authlogic and functional tests - Authlogic::Session::Activation::NotActivatedError: You must activate

Im getting the errors below despite following the documentation.
In test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "authlogic/test_case"
require 'test_help'
require 'shoulda'
require File.dirname(__FILE__) + "/factories"
In my functional test
require 'test_helper'
class SentencesControllerTest < ActionController::TestCase
setup do
:activate_authlogic
end
context "logged in" do
setup do
#user = Factory(:user)
UserSession.create(#user.id)
end
context "on GET to :new" do
setup do
get :new
end
should "present form with text field" do
assert_select('form#new_sentence') do
assert_select('textarea#sentence_text')
end
end
end
end #context logged in.
end
in environments.rb
config.gem "authlogic"
Im not sure why it isnt working. Can anyone help out on this?
Authlogic::Session::Activation::NotActivatedError: You must activate the Authlogic::Session::Base.controller with a controller object before creating objects
authlogic (2.1.3) lib/authlogic/session/activation.rb:47:in `initialize'
authlogic (2.1.3) lib/authlogic/session/klass.rb:64:in `initialize'
authlogic (2.1.3) lib/authlogic/session/scopes.rb:79:in `initialize'
authlogic (2.1.3) lib/authlogic/session/existence.rb:29:in `new'
authlogic (2.1.3) lib/authlogic/session/existence.rb:29:in `create'
test/functional/sentences_controller_test.rb:11:in `__bind_1270172858_922804'
shoulda (2.10.3) lib/shoulda/context.rb:380:in `call'
shoulda (2.10.3) lib/shoulda/context.rb:380:in `run_current_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:379:in `each'
shoulda (2.10.3) lib/shoulda/context.rb:379:in `run_current_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:371:in `run_all_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:375:in `run_parent_setup_blocks'
shoulda (2.10.3) lib/shoulda/context.rb:359:in `test: logged in on GET to :new should present form with text field. '
/opt/rubymine/rb/testing/patch/testunit/test/unit/ui/testrunnermediator.rb:36:in `run_suite'
/opt/rubymine/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:215:in `start_mediator'
/opt/rubymine/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:191:in `start'
Should:
class SentencesControllerTest < ActionController::TestCase
setup do
:activate_authlogic
end
...
be:
class SentencesControllerTest < ActionController::TestCase
def setup # setup should be its own method, prefixed with "def"
activate_authlogic # note the lack of a ":"
end
...
If, alternatively, you're following the Rails testing tutorial, it may have a single-line setup deal like:
setup :activate_authlogic # note the USE of a ":" here - not sure why it's different between this and when you put it in its own method but that might be the answer for you

Resources