in my Gemfile:
group :test do
gem 'minitest-spec-rails'
end
my test file in rail_root/test/functional/publisher_controller_test.rb
# -*- encoding : utf-8 -*-
describe PublisherController do
describe "GET #signin" do
it "responds successfully with an HTTP 200 status code" do
get :signin
assert_response :success
end
end
end
my unit test worked fine, but when I run
ruby -Itest test\functional\publisher_controller_test.rb"
it went wrong , here is the error:
test/functional/publisher_controller_test.rb:2:in `<main>':
uninitialized constant PublisherController (NameError).
I just don't know why it can find my model but can not find the controller.
Rails expects controller names to be plural, like so:
PublishersController
Your test is using a singular controller name, PublisherController, which doesn't exist. This can be caused by a typo when using generators.
To fix it, change Publisher to Publishers. Remember to change the filename as well.
Related
I have a simple test but the describe keyword is not working in Sorbet tests.
The error I'm receiving on these methods:
Method `describe` does not exist on `T.class_of(<root>)`7003
RSpec.describe(Model) do
describe 'my test' do
before(:each) do # .before error
user = FactoryBot.create(:user)
end
it 'can fill in all fields' do # .it errors
end
end
end
I think I need to tell Sorbet some how that this is called in the context of spec_helper.rbbut I'm not sure how to do that.
I've already installed this gem rspec-sorbet and ran
spec/spec_helper.rb
require 'rspec/sorbet'
To silence the errors, I ran this:
RSpec.describe(Model) do
T.bind(self, T.untyped)
# T.bind(self, RSpec) This does not work either
end
just starting to learn RSpec and TDD, and can't figure out why it's don't work at all.
#spec/api/event_api_spec.rb
describe 'Messages API' do
it 'check response' do
get 'api.mydomain.dev/events'
json = JSON.parse(response.body)
# test for the 200 status-code
expect(response).to be_success
end
end
I have create my API on api.mydomain.dev and my folder structure looks like app/controllers/api/events_controller.rb
So when I tried to run bundle exec rspec it's shown that
NoMethodError:
undefined method `get' for #<RSpec::ExampleGroups::MessagesAPI:0x007fc34900cee0>
if I'm trying to make smth like Event.creat!(:name => 'My Event') in My Spec file #spec/api/event_api_spec.rb it says
NameError:
uninitialized constant Event
So i don't understand How require my app/controllers/api/events_controller.rb file to the Spec file to get instance of my Event Class to get it work.
With default controllers it's work fine, I only interesting in API setup, thx
"get" is a method available on controller specs, try with something like
describe 'Messages API', type: :controller do
to tell rspec you are testing something like a controller, or maybe just do
describe EventsController do
About the "uninitialized constant Event", try with this at the begning of spec/api/event_api_spec.rb
require 'rails_helper'
Hope this helps, but your post is a little confusing, is not clear if your api is online or what, you shouldn't test agains the online api, you should test locally, specs should never communicate with the real world unless it's really really necessary.
I have this in my gemfile:
group :development, :test do
gem 'byebug'
gem 'rspec-rails', '~>3.0'
gem 'rspec-its'
gem 'factory_girl_rails'
gem 'json_spec'
gem "rspec_json_schema_matcher"
gem 'faker'
end
i have this request spec in spec/request/posts/show_spec.rb:
require 'rspec/its'
require 'spec_helper'
require 'rails_helper'
RSpec.describe 'GET /posts/:id', :type => :request do
let(:user) {create(:user)}
let(:guest) {create(:user, :as_guest)}
let(:post) {create(:post)}
let(:id) {post.id}
before(:each) {get "/posts/#{id}"}
context "when the post exists" do
expect(response).to have_http_status(:success)
end
context 'when a post is not found with the ID' do
let(:id) {-1}
expect(response).to have_http_status(:not_found)
end
end
when i run bundle exec rspec I get this error:
undefined local variable or method `response' for #<Class:0x00000002016470> (NameError)
What am I doing wrong?
response is not available on an example group (e.g. a describe or context block). It is only available from within individual examples (e.g. it blocks) or from constructs that run in the scope of an example (e.g. before, let, etc). (RSpec::Core::ExampleGroup::WrongScopeError)
Change
context "when the post exists" do
expect(response).to have_http_status(:success)
end
to
context "when the post exists" do
it 'status code is 200'
expect(response).to have_http_status(:success)
end
end
Or, even shorter, seeing that you use its
context "when the post exists" do
its(:response) { is_expected.to have_http_status(:success) }
end
PS: you get the error above when you run your code as a controller spec. For some reason running it with the request type throws the way less descriptive error that you are getting.
Slightly piggybacking off the comments of the answer, but I have had these issues each time setting up a new environment. Even so, I have a list of rules I follow to track down such issues.
Here are the 3 things to check for when making sure you are getting the proper helpers for your tests:
If you are using the infer_spec_type_from_file_location! option to omit the need for the type metadata, make sure that your folder names are named or pluralized correctly, and that your spec files are in the correct folder for the functionality you wish to access.
If the problem exists where your have your spec in the correct folder, or if you are using the type metadata in your top-most describe block, you are missing a required module somewhere (like the OP missing the 'rspec/rails' in their rails_helper). Double check your rails/spec_helper files, and make sure that the rails_helper is being requires in your .rspec file or spec file.
Lastly, if you are missing functionality that comes from an included module that needs more than a require and that you see has been included, make sure that it is set to the correct spec type. e.g. Devise test modules that have the option to set the type (type: :controller)
a. If this is the case and you need one of these type-based includes to cover more than one spec type, include it again with the 2nd type set to its type, or remove the type entirely if you don't care about your test speeds.
I hope that helps someone figure out issues in the future.
I'm followind the Rails Tutorial but have a problem in section 3.2.1, just before figure 3.6.
When running
$ bundle exec rspec spec/requests/static_pages_spec.rb
I get failure
Failures:
1) StaticPages GET /static_pages works! (now write some real specs)
Failure/Error: get static_pages_index_path
NameError:
undefined local variable or method `static_pages_index_path' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe7592b33b8>
# ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.00454 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # StaticPages GET /static_pages works! (now write some real specs)
here are the files :
spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
end
app/views/static_pages/home.html.erb
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
config/routes.rb
SecondApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
end
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.3'
group :development do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.9.0'
gem 'guard-rspec', '0.5.5'
end
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
gem 'rspec-rails', '2.9.0'
gem 'capybara', '1.1.2'
gem 'growl', '1.0.3'
end
group :production do
gem 'pg', '0.12.2'
end
Any idea about what I did wrong ?
I was having this problem as well; the tutorial seems to have missed a step.
in static_pages_spec.rb, the line:
get static_pages_index_path
...should be changed to:
get static_pages_home_path
This is because there is no index method in static_pages_controller.rb. The index is instead called home.
I reviewed your code, however, and it seems your static_pages_spec.rb file does not match the tutorial, but I guess you're copying the code from another place? I do not see static_pages_index_path anywhere, except in your console error text, which seems odd.
This is my static_pages_spec.rb in its entirety (at this stage), which passes the test:
require 'spec_helper'
describe "StaticPages" do
describe "GET /static_pages" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get static_pages_home_path
response.status.should be(200)
end
end
end
After this, the tutorial (in Listing 3.12) superseeds this step, changing static_pages_spec.rb altogether, although forgetting to explicitly recommend the change. This makes my code above irrelevant, but hopefully it explains your error.
Have a closer look at your spec/requests/static_pages_spec.rb. Please make sure you've deleted get static_pages_index_path line.
You have to update your app/views/static_pages/help.html.erb page to contain 'Sample App' in the same way as you have done with home.html.erb.
I had the same problem and I figured it out in the following way. I am also a newbie (first Stack Overflow post... nervous), so I don't have the jargon down:
The reason I was getting this error is that I didn't create the staticpages controller described in Listing 3.4 of the tutorial (I think I deleted it messing around with the practice commands that follow Listing 3.4 teaching you how to ahem, delete things). The way to check if you don't have the staticpages controller is to go to:
sample_app/controllers/ and so I only had the application_controller.rb file there and no static_pages_controller.rb.
So you have to run the rails generate controller StaticPages home help --no-test-framework
command and get that controller in there.
You can double-check your work by going to localhost:3000/static_pages/home and seeing if there is actually something there.
Then edit, per the tutorial, the home.html.erb files and check back to static_pages/home to see if it actually says "Sample App".
If static_pages/home actually says "Sample App" and the test is still a failure when you run it, then only God can help you. Or maybe someone else on Stack Overflow. Good luck.
Have you deleted the public/index.html? This could be causing the problem.
When you add "config.include Capybara::DSL" to the bottom of the RSpec helper file, in the section right before this, you may have forgotten to save the page. That is done by pressing the Command + S keys together. Sometimes it is also a good idea to restart your Rails Server if something doesn't work the way you expect it to the first time.
The problem is that you are trying to run a generated spec that doesn't actually work yet. The tutorial tells you to replace the body of that spec with this:
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
Note that it says "visit", not "get", and so on...
This happened because the spec generator (rails generate integration_test static_pages) assumes that there is a valid RESTful resource with lots of named routes, including _index_path, but that's simply not the case for this controller in this tutorial.
I was facing a similar problem. When you edit the static_pages_spec.rb, it looks like you typed this command (in listings 3.9)
<editor name> static_pages_spec.rb
whereas you had to type
<editor name> spec/requests/static_pages_spec.rb
This will surely solve your problem.
Well, I'm doing some testing right with Rails+Rspec+Shoulda.
When I do a test like the following:
context #user do
describe 'Validation' do
describe :name
it { should allow_value('something').for :name }
end
end
end
When it fails, Rspec just output:
1) Validation name Valid
Failure/Error: it { should allow_value(value).for :name }
Did not expect errors when name is set to "something", got error:
# ./spec/models/user_spec.rb:4:in `block (3 levels) in <top (required)>'
It even says got error: but it doesn't output it! I actually know there is a validation error there, but I want Rspec to tell me that, how I would know what is failing to validate then?
What am I doing wrong? Is that the expected behaviour? I have to overwrite the helpers?
I dug into the Shoulda code and I found that it doesn't show the errors when checking for positive assert. But them are loaded into the #errors variable. So I just monkey patched the one method that defines the output:
module Shoulda
module ActiveRecord
module Matchers
def failure_message
"Did not expect #{expectation}, got error: \n#{#expected_message ? #matched_error : #errors.join("\n ")}"
end
end
end
end
The original said:
"Did not expect #{expectation}, got error: #{#matched_error}"
I saved it to /lib/shoulda/activerecord/matchers.rb and loaded it with config.autoload_paths += Dir["#{config.root}/lib/**/"]
Hope this helps someone with the same issue ^^
Yup welcome to spec testing so you need to recreate the error in console if you want the error, rspec is not a debugger just a test suite.
I run into this a lot