ruby on rails - Minitest - NoMethodError: undefined method `get' -
i stuck error when run simple test minitest-rails gem. have rails 4.1.5 , minitest 5.4.0
rake test:controllers
1) error: dashboardcontroller::index action#test_0001_anonymous: nomethoderror: undefined method get' #<#<class:0x00000008e28170>:0x00000008eeb9b8> test/controllers/dashboard_controller_test.rb:6:in
block (3 levels) in '
test:
require "test_helper" describe dashboardcontroller context "index action" before :index end { must_respond_with :success } "must render index view" must_render_template :index end end end
my test_helper:
env["rails_env"] = "test" require file.expand_path("../../config/environment", __file__) require "rails/test_help" require "minitest/rails" require "minitest/rails/capybara" class minitest::spec class << self alias :context :describe end end class requesttest < minitest::spec include rails.application.routes.url_helpers register_spec_type(/request$/, self) end class actiondispatch::integrationtest # register "request" tests handled integrationtest register_spec_type(/request( ?test)?\z/i, self) end class activesupport::testcase activerecord::migration.check_pending! # setup fixtures in test/fixtures/*.(yml|csv) tests in alphabetical order. # # note: you'll still have declare fixtures explicitly in integration tests # -- not yet inherit setting fixtures :all # add more helper methods used tests here... extend minitest::spec::dsl end
there many things wrong doing. understand want use minitest's spec dsl in rails tests, correct? looks doing things accomplish don't need do. don't understand why half code in test_helper.rb
file there. suspect have other code doing things not being shown.
here did reproduce setup:
$ echo "creating new rails app" ☣ [rails41:rails41] $ rails new undefined_get ☣ [rails41:rails41] $ cd undefined_get/ $ echo "generate dashboard controller" $ rails g controller dashboard index $ echo "add minitest-rails dependencies" $ echo 'gem "minitest-rails"' >> gemfile $ echo 'gem "minitest-rails-capybara"' >> gemfile $ bundle install $ echo "the test runs fine now:" $ rake test run options: --seed 47210 # running: . finished in 0.457972s, 2.1835 runs/s, 2.1835 assertions/s. 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips $ echo "update test code , test_helper code" $ echo "use whatever editor want. not shown here." $ echo "now rerun tests:" $ rake test rake aborted! nomethoderror: undefined method `context' #<class:0x007f860258ae50>
the error different yours. aliased method context
describe
in test_helper.rb
file, unfortunately object aliased not in inheritance chain rails test objects. rails test objects extend minitest::spec::dsl
, not inherit minitest::spec
. so, suspicious code provided indeed producing results have presented. said, here code in test_helper.rb
run test:
env['rails_env'] ||= 'test' require file.expand_path('../../config/environment', __file__) require 'rails/test_help' require "minitest/rails" require "minitest/rails/capybara" class activesupport::testcase # setup fixtures in test/fixtures/*.yml tests in alphabetical order. fixtures :all # allow context used describe class << self alias :context :describe end # add more helper methods used tests here... end
this standard test_helper.rb
2 changes. first, has requires minitest-rails , minitest-rails-capybara. need in order enable minitest spec dsl in rails tests. second, adds alias context
describe
on activesupport::testcase
, basis rails tests. if want add tests not inherit activesupport::testcase
can alias on minitest::spec
, not use context
within controller tests.
still here? okay. why did code give different error mine? test object used controller tests isn't actioncontroller::testcase
. because error undefined method get
. get
method actioncontroller::testcase
defines, , not on minitest::spec
. so, somehow messed minitest configuration. simple way make sure tests using correct test objects add additional assertion test. this:
require "test_helper" describe dashboardcontroller context "index action" before # make sure using correct test class self.class.ancestors.must_include actioncontroller::testcase # continue setup :index end { must_respond_with :success } "must render index view" must_render_template :index end end end
if first assertion fails know have done wrong in configuration.
Comments
Post a Comment