Error when I run bundle exec in terminal - ruby-on-rails

When I run bundle exec rake test it comes up with an error in the terminal. It can't seem to get passed this step
# Running:
.......EEE.......
Finished in 0.420936s, 40.3862 runs/s, 61.7672 assertions/s.
1) Error:
StaticPagesControllerTest#test_should_get_help:
NameError: uninitialized constant ApplicationController::TestCase
app/controllers/static_pages_controller.rb:1:in `<top (required)>'
2) Error:
StaticPagesControllerTest#test_should_get_home:
NameError: uninitialized constant ApplicationController::TestCase
app/controllers/static_pages_controller.rb:1:in `<top (required)>'
3) Error:
StaticPagesControllerTest#test_should_get_about:
NameError: uninitialized constant ApplicationController::TestCase
app/controllers/static_pages_controller.rb:1:in `<top (required)>'
17 runs, 26 assertions, 0 failures, 3 errors, 0 skips
I tried fixing the problem to the code but have not come up with a solution.
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
assert_select "title", "Home | Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get :help
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get :about
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
end

The first line of you test code should be:
class StaticPagesControllerTest < ActionController::TestCase
It's ActionController - not ApplicationController.

Related

RoR rspec-rails error NoMethodError: Undefined method ‘get’

everybody!
I am doing RoR requests test and I am getting this error:
Failures:
1) Users GET #index is a success
Failure/Error: before(:example) { get "/users" }
NoMethodError:
undefined method `get' for #<RSpec::ExampleGroups::Users::GETIndex:0x000055a33f143f60>
Did you mean? gets
gem
# ./spec/requests/users_spec.rb:6:in `block (3 levels) in <top (required)>'
My code is:
RSpec.describe 'Users', type: :request do
describe 'GET #index' do
before(:example) { get "/users" }
it 'is a success' do
expect(response).to have_http_status(:ok)
end
it "renders 'index' template" do
expect(response).to render_template(:index)
end
it "shows the rendered text 'Welcome to the Ruby on Rails Blog'" do
expect(response).to include('Welcome to the Ruby on Rails Blog')
end
end
end
I did not include require ‘rails_helper’ in the spec file, because when I do this I am getting this error
ChildProcess::MissingFFIError:
FFI is a required pre-requisite for Windows or posix_spawn support in the ChildProcess gem. Ensure the `ffi` gem is installed. If you believe this is an error, please file a bug at http://github.com/enkessler/childprocess/issues
ruby version => ruby 3.1.1p18
rails version => Rails 7.0.2.4
RSpec version => RSpec 3.11

Why is my test framework unable to find a URL defined in my routes file?

I'm using Rails 5 and minutest and trying to write a test for a controller. The test is
# issues_controller_test.rb
class IssuesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "logged in should get issues page" do
sign_in users(:one)
test_stop_id = 1
test_line_id = 1
get new_issue, :stop_id => test_stop_id, :line_id => test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end
end
The method in question attempts to access this page, defined in my rails routes ...
new_issue GET /issues/new(.:format) issues#new
Yet when I run the test, I get this error
# Running:
.E
Error:
IssuesControllerTest#test_logged_in_should_get_issues_page:
NameError: undefined local variable or method `new_issue' for #<IssuesControllerTest:0x007f816759b330>
test/controllers/issues_controller_test.rb:10:in `block in <class:IssuesControllerTest>'
bin/rails test test/controllers/issues_controller_test.rb:6
Finished in 0.103956s, 19.2389 runs/s, 9.6195 assertions/s.
Why is the test framework unable to find a method defined in my routes file?
new_issue that way is just a local variable defined nowhere, try specifying it as a working url with new_issue_url:
test 'logged in should get issues page' do
sign_in users :one
test_stop_id = 1
test_line_id = 1
get new_issue_url, stop_id: test_stop_id, line_id: test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end

Section 5.6 Exercise 4 in Hartl Tutorial

I am trying to complete exercise 4 in Chapter 5 of the Hartl tutorial and I am getting these two errors when I run $bundle exec rake test:
Started
ERROR["test_layout_links", SiteLayoutTest, 2015-03-31 09:33:35 +0000]
test_layout_links#SiteLayoutTest (1427794415.88s)
NoMethodError: NoMethodError: undefined method `full_title' for # <SiteLayoutTest:0x00000006cee338>
test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
test/integration/site_layout_test.rb:13:in `block in <class:SiteLayoutTest>'
ERROR["test_full_title_helper", ApplicationHelperTest, 2015-03-31 09:33:35 +0000]
test_full_title_helper#ApplicationHelperTest (1427794415.99s)
ArgumentError: ArgumentError: wrong number of arguments (0 for 1)
app/helpers/application_helper.rb:2:in `full_title'
test/helpers/application_helper_test.rb:5:in `block in <class:ApplicationHelperTest>'
app/helpers/application_helper.rb:2:in `full_title'
test/helpers/application_helper_test.rb:5:in `block in <class:ApplicationHelperTest>'
7/7: [====================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.57888s
7 tests, 14 assertions, 0 failures, 2 errors, 0 skips
What do these errors mean, and how do I go about fixing the errors?
This is my test/integration/site_layout_test.rb code:
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
get signup_path
assert_select "title", full_title("Sign up")
end
end
And this is my app/helpers/application_helper.rb code:
module ApplicationHelper
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
" #{page_title} | #{base_title}"
end
end
end
This is my test/helpers/application_helper_test.rb file:
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
test "full title helper" do
assert_equal full_title, "Ruby On Rails App"
assert_equal full_title("Help"), "Help | Ruby On Rails App"
end
end
What do these errors mean, and how do I go about fixing the errors?
NoMethodError: undefined method 'full_title' for #<SiteLayoutTest:0x00000006cee338>
test/integration/site_layout_test.rb:13
On line 13, you called a method that doesn't exist.
See http://ruby-doc.org/core-2.2.0/NoMethodError.html
ArgumentError: wrong number of arguments (0 for 1)
app/helpers/application_helper.rb:2:in 'full_title'
test/helpers/application_helper_test.rb:5
On line 5, you need to pass exactly one argument.
See http://ruby-doc.org/core-2.2.0/ArgumentError.html
See where the line numbers are in the error message?
I recommend reading an introductory ruby book, like http://ruby-doc.com/docs/ProgrammingRuby/ before (or while) learning rails.
Good luck!
In case anyone comes across the same error when they are going through the Michael Hartl 3rd edition...
I found the error in the app/helpers/application_helper.rb code. I needed to add an empty string for page_title, changing 'def full_title(page_title)' to 'def full_title(page_title= '')'
module ApplicationHelper
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{page_title} | #{base_title}"
end
end
end

chapter 8 .micheal hartl ror 3rd edition

hi i am presently on chapter 8 micheal hartl Learning Rails 3rd edition.
presently i am stucked in chapter 8 with above errors .I will highly appreciate if somebuddy help me out with it. Thanks in advance
when i run bundle exec rake test it comes up with
sample_app git:(log-in-log-out) ✗ bundle exec rake test
Run options: --seed 16515
# Running:
......E............
Finished in 0.944425s, 20.1181 runs/s, 40.2361 assertions/s.
1) Error:
SiteLayoutTest#test_layout_links:
NoMethodError: undefined method `full_title' for #<SiteLayoutTest:0x007ff8bea694b0>
test/integration/site_layout_test.rb:12:in `block in <class:SiteLayoutTest>'
19 runs, 38 assertions, 0 failures, 1 errors, 0 skips
whereas my file posses the following
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "layout links" do
get root_path
assert_template 'static_pages/home'
assert_select "a[href=?]", root_path, count: 2
assert_select "a[href=?]", help_path
assert_select "a[href=?]", about_path
assert_select "a[href=?]", contact_path
get signup_path
assert_select "title", full_title("Sign up")
end
end
full_title is a helper method defined in application_helper.rb. Check out https://www.railstutorial.org/book/_single-page#code-title_helper

Ruby Debug "no such file to load --spec_helper"

Noob who may be missing something obvious ...
I'm trying to debug an Rspec file. The Rspec file is stripped down at this point:
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
describe "when not signed in" do
before(:each) do
get :home
end
it "should be successful" do
response.should be_success
end
it "should have a vendor section" do
response.should have_selector("h1", :content => "Vendor")
end
it "should have a hospital section" do
response.should have_selector("h1", :content => "Hospital")
end
end
end
I make the following call from the command line:
rdebug spec/controllers/pages_controller_spec.rb
The debugger runs, but throws the following error:
> require 'spec_helper'
<internal:lib/rubygems/custom_require>:29:in `require'
<internal:lib/rubygems/custom_require>:29:in `require'
/home/kevin/.rvm/bin/rails_projects/evaluationrx/spec/controllers/pages_controller_spec.rb:1:in `<top (required)>'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:125:in `debug_load'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:125:in `debug_program'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/gems/ruby-debug19-0.11.6/bin/rdebug:412:in `<top (required)>'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/bin/rdebug:19:in `load'
/home/kevin/.rvm/gems/ruby-1.9.2-p136#rails3tutorial/bin/rdebug:19:in `<main>'
Uncaught exception: no such file to load -- spec_helper
Rspec without the debugger without a problem. I'm using Rspec 2.3.0, ruby-debug19 (0.11.6), Rails 3.0.3 and ruby 1.9.2. Why can't the debugger see the spec_helper file?
Make sure you've run
rails generate rspec:install
to create the spec_helper.rb file.
I assume your spec_helper.rb resides in the spec directory? Try:
require_relative '../spec_helper'

Resources