Rails 6.0.0.beta3
rspec 3.8.0
I've just installed rspec-rails on my Rails app. I ran rails g rspec:install and it generated spec/spec_helper.rb, spec/rails_helper.rb and .rspec
The only thing I changed was uncommenting the suggested settings in spec_helper
I'm using gruf to run a gRPC server, instead of a normal HTTP server.
I've also installed gruf-rspec intending to use that to test my gruf controllers.
My gruf controller is at app/rpc/controllers/users_controller.rb following the gruf documentation. Also the compiled protobuf file is at app/rpc/users_services_pb.rb
This is the class signature of the controller:
require 'users_services_pb'
require_relative 'permission_helper'
class UsersController < Gruf::Controllers::Base
bind ::Sil::Rev79::Users::Service
...
end
My problem is that in my test the described_class is nil
Here is my test
# spec/rpc/users_contollers_spec.rb
require 'rails_helper'
require 'users_services_pb'
RSpec.describe 'UsersController' do
describe 'list_users' do
it 'succeeds' do
expect(described_class).not_to be_nil
end
end
end
The test fails.
Why is described_class nil and how can I fix this?
Remove the quotation marks. It shouldn't be a string.
Rspec.describe UsersController do
# insert code
end
Related
I was stuck with error when i run very simple test with minitest-rails gem.
I have rails 4.1.5 and minitest 5.4.0
rake test:controllers
1) Error:
DashboardController::index action#test_0001_anonymous:
NoMethodError: undefined method get' for #<#<Class:0x00000008e28170>:0x00000008eeb9b8>
test/controllers/dashboard_controller_test.rb:6:inblock (3 levels) in '
Test:
require "test_helper"
describe DashboardController do
context "index action" do
before do
get :index
end
it { must_respond_with :success }
it "must render index view" do
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 to be handled by IntegrationTest
register_spec_type(/Request( ?Test)?\z/i, self)
end
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
extend MiniTest::Spec::DSL
end
There are many things wrong with what you are doing. As I understand it you want to use Minitest's spec DSL in your Rails tests, correct? It looks like you are doing things to accomplish this that you don't need to do. I don't understand why half that code in your test_helper.rb file is there. I also suspect that you have other code doing things that are not being shown.
Here is what I did to reproduce your setup:
$ echo "Creating a new Rails app"
☣ [rails41:rails41] $ rails new undefined_get
☣ [rails41:rails41] $ cd undefined_get/
$ echo "Generate a 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 to your test code and test_helper code"
$ echo "Use whatever editor you want. Not shown here."
$ echo "Now rerun the tests:"
$ rake test
rake aborted!
NoMethodError: undefined method `context' for #<Class:0x007f860258ae50>
The error I get is different than yours. You aliased the method context to describe in your test_helper.rb file, but unfortunately the object you aliased is not in the inheritance chain for the rails test objects. The rails test objects extend Minitest::Spec::DSL, but they do not inherit from Minitest::Spec. So, I am strongly suspicious that the code you provided is indeed producing the results you have presented. That said, here is the code in my test_helper.rb that will run your 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 all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Allow context to be used like describe
class << self
alias :context :describe
end
# Add more helper methods to be used by all tests here...
end
This is the standard test_helper.rb with two changes. First, it has the requires for minitest-rails and minitest-rails-capybara. That is all you need to do in order to enable the Minitest spec DSL in your rails tests. Second, it adds the alias for context to describe on ActiveSupport::TestCase, which is the basis for all the rails tests. If you want to add tests that do not inherit from ActiveSupport::TestCase then you can also alias it on Minitest::Spec, but that will not help you use context within your controller tests.
Still here? Okay. So why did your code give you a different error than mine? Likely the test object used for your controller tests isn't ActionController::TestCase. I say that because your error was undefined method get. The get method is something that ActionController::TestCase defines, and is not on Minitest::Spec. So, you somehow messed up your Minitest configuration. A simple way to make sure that your tests are using the correct test objects is to add an additional assertion to your test. Like this:
require "test_helper"
describe DashboardController do
context "index action" do
before do
# Make sure we are using the correct test class
self.class.ancestors.must_include ActionController::TestCase
# Continue with setup
get :index
end
it { must_respond_with :success }
it "must render index view" do
must_render_template :index
end
end
end
If that first assertion fails then you know you have done something wrong in your configuration.
My java web application is running on tomcat at http://localhost:8080/
Writing my first spec, home_spec:
require 'spec_helper'
describe "home" do
it "should render the home page" do
visit "/"
page.should have_content("hello world")
end
end
And running:
rspec
I get:
F
Failures:
1) home should render the home page
Failure/Error: visit "/"
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x242870b7>
# ./spec/home/home_spec.rb:7:in `(root)'
Finished in 0.012 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/home/home_spec.rb:6 # home should render the home page
Shouldn't this work because I have included capybara in the spec_helper?
How will it know to visit the correct url? what if my url is localhost:3030 or localhost:8080?
My gemfile:
source 'http://rubygems.org'
gem "activerecord"
gem "rspec"
gem "capybara"
gem "activerecord-jdbcmysql-adapter"
My spec_helper:
require 'capybara/rspec'
Regarding to rspec issues (https://github.com/rspec/rspec-rails/issues/360)
you should put
config.include Capybara::DSL
in spec_helper.rb, inside the config block.
The default directory that Capybara::RSpec now looks at to include the Capybara::DSL and Capybara::RSpecMatchers is changed from requests to features.
After I renamed my requests directory to features I got the matcher and DSL methods available again without having to explicitly include them.
See the following commit
Also make sure your tests are in the /spec/features directory. According to rspec-rails and capybara 2.0, Capybara v2 and higher will not be available by default in RSpec request specs. They suggest to "...move any tests that use capybara from spec/requests to spec/features."
By default the capybara DSL is included automatically if the file is in spec/requests, spec/integration or if the example group has :type => :request.
Because your file is in spec/home the capybara helpers aren't being included. You can either conform to one of the patterns above or adding include Capybara::DSL should also do the trick (you might also need to replicate some of the before(:each) stuff that would be setup.)
First check it out
If you are not success,
Add this code your end of the your spec helper actually out of the RSpec.configure block as well
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
1) Add to ‘rails_helper’ config:
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
And comment out the `require 'spec_helper'` line.
2) Add to 'spec_helper':
require 'rails_helper'
My java web application is running on tomcat at http://localhost:8080/
Writing my first spec, home_spec:
require 'spec_helper'
describe "home" do
it "should render the home page" do
visit "/"
page.should have_content("hello world")
end
end
And running:
rspec
I get:
F
Failures:
1) home should render the home page
Failure/Error: visit "/"
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x242870b7>
# ./spec/home/home_spec.rb:7:in `(root)'
Finished in 0.012 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/home/home_spec.rb:6 # home should render the home page
Shouldn't this work because I have included capybara in the spec_helper?
How will it know to visit the correct url? what if my url is localhost:3030 or localhost:8080?
My gemfile:
source 'http://rubygems.org'
gem "activerecord"
gem "rspec"
gem "capybara"
gem "activerecord-jdbcmysql-adapter"
My spec_helper:
require 'capybara/rspec'
Regarding to rspec issues (https://github.com/rspec/rspec-rails/issues/360)
you should put
config.include Capybara::DSL
in spec_helper.rb, inside the config block.
The default directory that Capybara::RSpec now looks at to include the Capybara::DSL and Capybara::RSpecMatchers is changed from requests to features.
After I renamed my requests directory to features I got the matcher and DSL methods available again without having to explicitly include them.
See the following commit
Also make sure your tests are in the /spec/features directory. According to rspec-rails and capybara 2.0, Capybara v2 and higher will not be available by default in RSpec request specs. They suggest to "...move any tests that use capybara from spec/requests to spec/features."
By default the capybara DSL is included automatically if the file is in spec/requests, spec/integration or if the example group has :type => :request.
Because your file is in spec/home the capybara helpers aren't being included. You can either conform to one of the patterns above or adding include Capybara::DSL should also do the trick (you might also need to replicate some of the before(:each) stuff that would be setup.)
First check it out
If you are not success,
Add this code your end of the your spec helper actually out of the RSpec.configure block as well
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
1) Add to ‘rails_helper’ config:
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
And comment out the `require 'spec_helper'` line.
2) Add to 'spec_helper':
require 'rails_helper'
In my Rails app I've added the following files:
app/models/baz.rb
lib/presenters/foo_presenter.rb
lib/presenters/foo_presenter/bar.rb
spec/models/baz_spec.rb
spec/lib/presenters/foo_presenter/bar_spec.rb
The contents of lib/presenters/foo_presenter.rb is something like:
module Presenters
module FooPresenter
def self.render
# do stuff
end
end
end
The contents of lib/presenters/foo_presenter/bar.rb is like:
module Presenters
class FooPresenter::Bar
def baz
# do stuff
end
end
end
The contents of spec/lib/presenters/foo_presenter/bar_spec.rb is like:
require 'spec_helper'
module Presenters::FooPresenter
describe Bar do
# some tests
end
end
Then I have a spec file in spec/models/baz_spec.rb:
require 'spec_helper'
describe Baz do
it 'works' do
Presenters::FooPresenter.render
end
end
(The contents of app/models/baz.rb is not relevant to this issue)
The problem is when I run rspec spec/models/baz_spec.rb it works fine without spork, but when spork is running, I get an error like:
NameError: undefined method `render' for Presenters::FooPresenter:Module
I traced through the code a bit and noticed that when rspec loads spec/lib/presenters/foo_presenter/bar_spec.rb it causes Rails to autoload lib/presenters/foo_presenter/bar.rb and so at that point Presenters::FooPresenter::Bar is loaded, but then when baz_spec.rb runs, lib/presenters/foo_presenter.rb has never been loaded and thus the exception. But this only happens if spork is running. The quick fix was to require 'foo_presenter' in a file in config/initializers, but is there a cleaner solution that doesn't need the explicit require? My guess is the issue here is that Rails doesn't autoload lib/presenters/foo_presenter.rb because Presenters::FooPresenter has already been defined by bar_spec.rb.
A co-worker and I were faced with this problem today and we eventually found we needed Spork to reload the classes on every run. We used the each_run() method to do this:
Spork.each_run do
Dir[Rails.root.join("app/classes/**/*.rb")].each {|f| require f}
end
I create a simpe gem wich include a install generator, generator works fine but now I want to test it using rspec, I foud this gem, and try to test my generator, my spec code is:
require 'genspec'
require 'rosalie'
describe :install_generator do
it "should generate model" do
subject.should generate("message.rb")
end
end
rosalie is the name of may gem, now when I run it I got an error:
/stuff/work/my_projects/rosalie/lib/rosalie/engine.rb:2:in `': uninitialized constant Rosalie::Rails (NameError)
my engine.rb code is:
module Rosalie
class Engine < Rails::Engine
initializer "rosalie.models.messageable" do
ActiveSupport.on_load(:active_record) do
include Rosalie::Models::Messageable
end
end
end
end
anybody can help me with this problem?
You have to load your code before you include it somewhere.
Either require or autoload your main file.
Here is an example from my gem.
You need add these code in your spec_helper.rb, and require the spec_helper in each spec.
require File.expand_path("../dummy/config/environment", __FILE__)
require 'rspec/rails'