I got error on RSpec running my helper test on Rails Main Application (with further plugins)
1) MenuHelper maintence menu
Failure/Error: before { menu = build_menu_maintence() }
NameError:
undefined local variable or method `main_app' for #<RSpec::Core::ExampleGroup::Nested_3::Nested_1:0x007f92f561f280>
# ./app/helpers/menu_helper.rb:37:in `eval'
# (eval):1:in `block in build_menu_items'
# ./app/helpers/menu_helper.rb:37:in `eval'
# ./app/helpers/menu_helper.rb:37:in `block in build_menu_items'
# ./app/helpers/menu_helper.rb:23:in `each'
# ./app/helpers/menu_helper.rb:23:in `build_menu_items'
# ./app/helpers/menu_helper.rb:15:in `build_menu'
# ./app/helpers/menu_helper.rb:48:in `build_menu_maintence'
# ./spec/helpers/menu_helper_spec.rb:11:in `block (3 levels) in <top (required)>'
I had success with the following in a mountable Engine:
def main_app
Rails.application.class.routes.url_helpers
end
main_app.root_path
Related
I am trying to run a single test file: bundle exec rspec spec/models/user_spec.rb
But get following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: config.include ::Rails::Controller::Testing::TemplateAssertions, type: :controller
NameError:
uninitialized constant Rails::Controller
Did you mean? ApiController
# ./spec/rails_helper.rb:149:in `block in <top (required)>'
# ./spec/rails_helper.rb:61:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
Initially, I was getting following error:
An error occurred while loading ./spec/models/user_spec.rb.
Failure/Error: module Shoulda::Matchers::ActiveModel
NameError:
uninitialized constant Shoulda
# ./spec/support/matchers/validate_kept_of_matcher.rb:4:in `<top (required)>'
# ./spec/rails_helper.rb:51:in `block in <top (required)>'
# ./spec/rails_helper.rb:51:in `each'
# ./spec/rails_helper.rb:51:in `<top (required)>'
# ./spec/models/user_spec.rb:3:in `require'
# ./spec/models/user_spec.rb:3:in `<top (required)>'
No examples found.
But then it fixed after I added following to the test file.
require "shoulda/matchers"
I am newbie in Ruby/Rails world, can someone please give a direction?
Rails::Controller::Testing::TemplateAssertions was removed in Rails 5.
You can re-add the depreciated functionality by installing the Rails controller testing gem. However the use of controller specs, assigns and template assertions is discouraged by both the RSpec and Rails teams and is not very future proof.
The community accepted solution is to write request specs and stop poking inside your controllers.
I have created a mechanize task in rails. it is very simple
task :estimateone => :environment do
require 'mechanize'
mechanize = Mechanize.new
page = mechanize.get('https://www.theurbanlist.com/brisbane/a-list/50-brisbane-cafes-you-should-have-eaten-breakfast-at')
page.css('ol li a').each do |link|
link.click
end
end
but I get this error, what am I doing wrong?
rake aborted!
NoMethodError: undefined method `click' for #<Nokogiri::XML::Element:0x00007ffb4b281830>
/Users/jeremybray/RubymineProjects/OpportunityFinder/lib/tasks/getlead.rake:8:in `block (2 levels) in <top (required)>'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:204:in `block in each'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:203:in `upto'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/gems/nokogiri-1.8.4/lib/nokogiri/xml/node_set.rb:203:in `each'
/Users/jeremybray/RubymineProjects/OpportunityFinder/lib/tasks/getlead.rake:7:in `block in <top (required)>'
/Users/jeremybray/.rvm/gems/ruby-2.4.2#global/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in `eval'
/Users/jeremybray/.rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:15:in `<main>'
As click is a method of Mechanize, you should use your instance of it as the receiver of the click method, not the link, that's a Nokogiri one.
As the doc states, it receives the element, clicks it and returns the fetched page.
Try with:
-- snip --
page.css('ol li a').each do |link|
mechanize.click(link)
end
Page#links_with will return links, css will return html elements:
page.links_with(css: 'ol li a').each do |link|
link.click
end
I'm trying to write a simple test but my application is not detecting 'users' keyword when I'm using it in my Test functions:
post_controller_test.rb:
require 'test_helper'
class PostsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
test "should fetch facebook post" do
sign_in users(:one)
get(:save_posts_from_facebook_page,{'id'=>'my_id'},{'access_token'=>Rails.application.secrets.fb_access_token})
assert_response :success
end
end
test_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
include Devise::TestHelpers
# Add more helper methods to be used by all tests here...
end
users.yml:
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
email: hello#testing.com
encrypted_password: <%= Devise.bcrypt(User,'password') %>
links:
causes:
two:
email: MyString
password: MyString
links:
causes:
Output of rake/Error:
Run options: --seed 42684
# Running:
E
Finished in 0.010275s, 97.3279 runs/s, 0.0000 assertions/s.
1) Error:
PostsControllerTest#test_should_fetch_facebook_post:
NoMethodError: undefined method `users' for #<PostsControllerTest:0x000001042152f0>
test/controllers/posts_controller_test.rb:9:in `block in <class:PostsControllerTest>'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Update:
Note that I'm using MongoId.
When I add 'fixtures :all' to my test_helper.rb, I get:
rake
rake aborted!
NoMethodError: undefined method `fixtures' for ActiveSupport::TestCase:Class
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:7:in `<class:TestCase>'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/test_helper.rb:5:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/My Stuff/FreeFromBorders/f2b_website/test/controllers/posts_controller_test.rb:1:in `<top (required)>'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/Users/gautambajaj/.rvm/gems/ruby-2.1.2/gems/railties-4.2.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
Tasks: TOP => test:run
(See full trace by running task with --trace)
In your test_helper.rb file, you need to add fixtures :all:
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# rest of the codes in this file
end
Once you add that line in your test_helper.rb file as shown above, your test will pass.
Update
As Sergio Tulentsev mentioned in the comment, Mongodb doesn't support transactions, so transactional fixtures can't be used in your case. Take a look at this answer which states that and also this google group discussion as well.
I would recommend you to use factory_girl instead, which supports mongoid and is pretty awesome.
This seemed to be a flickering sort of a bug for a while,but now it's appearing consistently: when I run RSpec on a fairly simple ApplicationHelper spec, I get the following error:
% rspec --backtrace
1) ApplicationHelper renders Markdown from plain text
Failure/Error: expect(helper.md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
NameError:
undefined local variable or method `helper' for #<RSpec::ExampleGroups::ApplicationHelper_2:0x000001248d1218>
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-expectations-0f7b78587ab4/lib/rspec/matchers.rb:903:in `method_missing'
# ./spec/helpers/application_helper_spec.rb:4:in `block (2 levels) in <top (required)>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:148:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:148:in `block in run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `call'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `block (2 levels) in <class:Procsy>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-rails-480b173c9ad6/lib/rspec/rails/adapters.rb:67:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:292:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:292:in `instance_exec'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:430:in `block (2 levels) in run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `call'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:208:in `block (2 levels) in <class:Procsy>'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:432:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/hooks.rb:485:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:301:in `with_around_example_hooks'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example.rb:145:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:494:in `block in run_examples'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:490:in `map'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:490:in `run_examples'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/example_group.rb:457:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `block (2 levels) in run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `map'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:112:in `block in run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/reporter.rb:49:in `report'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:108:in `run_specs'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:86:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:70:in `run'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/lib/rspec/core/runner.rb:38:in `invoke'
# /Users/danielsh/.rvm/gems/ruby-2.1.1#project-staging-ng/bundler/gems/rspec-core-4219c4786f6f/exe/rspec:4:in `<top (required)>'
# /Users/danielsh/Dropbox/Project/Websites/Angular/bin/rspec:20:in `load'
# /Users/danielsh/Dropbox/Project/Websites/Angular/bin/rspec:20:in `<main>'
Here's the complete spec file (spec_helper is included as part of my .rspec file):
describe ApplicationHelper do
it 'renders Markdown from plain text' do
plaintext = '# Header'
expect(helper.md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
end
end
This was working up until recently, but I'm not certain what I could have done to break such a basic feature. I'm using edge versions of Rails and RSpec, but didn't see anything in their git repos to suggest that helper had been deprecated---and running rails g helper foo still generates a foo_helper_spec.rb file with instructions indicating that helper contains the helper itself. If anyone has any ideas, I'd be grateful for them!
I created a new Rails project with a fresh RSpec installation, and it led me to the problem. Apparently one of the recent betas introduced a configuration directive called config.infer_spec_type_from_file_location! that was missing from my slightly older spec_helper file; without it, RSpec wasn't guessing the spec type and mixing in the associated methods. Beware breaking changes!
add :type => :helper
so your code should look like
describe ApplicationHelper, type: :helper do
...
end
That's a weird error !! are you sure you required spec_helper in your spec ?
Anyway you could try without the helper method:
First you should first add to /spec/spec_helper.rb the following:
RSpec.configure do |config|
...
config.include ApplicationHelper
end
Then test without helper , so it will be:
describe ApplicationHelper do
it 'renders Markdown from plain text' do
plaintext = '# Header'
expect(md(plaintext)).to eq("<h1 id=\"header\">Header</h1>\n")
end
end
From the backtrace, it doesn't look like you're using the rspec-rails gem - just rspec-core and rspec-expectations.
rspec-rails is what provides the helper method to your helper specs. From inside a spec in my codebase:
(rdb:1) self.method(:helper).source_location
["/home/becky/.gem/ruby/2.0.0/gems/rspec-rails-2.14.1/lib/rspec/rails/example/helper_example_group.rb", 19]
Im using autotest-notification v2.3.4 and rspec v2.0.1 for writing tests in my rails v3.2.3 project on my machine running Ubuntu 12.04.In my pages_controller_spec.rb i have the following code
require 'spec_helper'
describe PagesController do
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
end
end
When i run the command rspec spec/ im getting the following error
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instanc\
e methods directly in RSpec::Rails::SetupAndTeardownAdapter instead. (called from <top (required)> at /home/kris/development/rails_projects/s\
ample_app/spec/controllers/pages_controller_spec.rb:3)
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instanc\
e methods directly in RSpec::Rails::TestUnitAssertionAdapter instead. (called from <top (required)> at /home/kris/development/rails_projects/\
sample_app/spec/controllers/pages_controller_spec.rb:3)
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instanc\
e methods directly in RSpec::Rails::ViewRendering instead. (called from <top (required)> at /home/kris/development/rails_projects/sample_app/\
spec/controllers/pages_controller_spec.rb:3)
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instanc\
e methods directly in RSpec::Rails::ControllerExampleGroup instead. (called from <top (required)> at /home/kris/development/rails_projects/sa\
mple_app/spec/controllers/pages_controller_spec.rb:3)
Failures:
1) PagesController GET 'contact' should be successful
Failure/Error: Unable to find matching line from backtrace
undefined method `run_all' for []:Array
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/hooks.rb:116:in `run_hook_filtered'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/example_group.rb:174:in `eval_before_alls'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/example_group.rb:229:in `run'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:26:in `block (2 levels) in run'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:26:in `map'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:26:in `block in run'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/reporter.rb:11:in `report'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:23:in `run'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:55:in `run_in_process'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:46:in `run'
# /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:10:in `block in autorun'
Finished in 0.00028 seconds
1 example, 1 failure
/home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/hooks.rb:116:in `run_hook_filtered': undefined method `run_all' for\
[]:Array (NoMethodError)
from /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/example_group.rb:213:in `eval_after_alls'
from /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/example_group.rb:236:in `run'
from /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:26:in `block (2 levels) in run'
from /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:26:in `map'
from /home/kris/.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:26:in `block in run'
What is causing this error and how can i fix it ?
Please Help
Thank You.
Try updating RSpec. This: ruby-forum.com/topic/788351 (and the linked github issue) suggests there is an issue with older versions of RSpec, and Ruby 1.9.3.