I get an issue when launching rspec
Failure/Error:
expect {
post :create, params: {tag: attributes_for(:tag)}
}.to change(Tag, :count).by(1)
NameError:
undefined local variable or method `matcher_name' for #<RSpec::Matchers::BuiltIn::Change:0x000000094f7348>
Did you mean? match_unless_raises
Edit :
it only occurs when I do
rspec
instead of
bundle exec rspec
Always use bundle exec with your project-related binaries. Without it, a wrong version is likely picked up. Check it.
rspec --version
bundle exec rspec --version
The console error log says probably you are supplying wrong method name, That's clear. Can you try changing the name of the method or variable which you are using?
You can also refer this for getting the difference between those two commands
bundle exec rspec VS rspec spec
Related
I am attempting to set up rspec to run only failures with rspec --only-failures. When I do this I get this error
block in fully_formatted_failed_examples': undefined method 'fully_formatted'
for #
<RSpec::Core::Notifications::ExampleNotification:0x00007fce3766b108> (NoMethodError)
When I run be rspec --only-failures I get this error:
bundler: failed to load command: rspec (/Users/mikeheft/.rbenv/versions/2.5.0/bin/rspec)
I have config.example_status_persistence_file_path = './spec/examples.txt' set up in spec_helper.rb
When I check out the source code on GitHub, I can see the line it's erroring out on, but I don't see a way on my end to fix this as it's in the gem it looks like. I have also opened an issue on Github.
According to this tutorial http://carlosplusplus.github.io/blog/2014/02/01/testing-rake-tasks-with-rspec/
to test rake tasks with rspec, one has to set
Rake.application = rake
in a before block.
However, I get the error
Failure/Error: Rake.application = rake
NameError:
uninitialized constant Rake
How can I fix this?
You need to require "rake" before the offending code. The tutorial you are using is based on a blog post by Thoughtbot, which includes the appropriate require and can be used as an example.
I am trying creating user-specific posts in the seeds file. the directions I am following tell me to reset the database. In the rails c (console) I entered:
rake db:reset
It gave me this error. Doesn't make sense.
NameError: undefined local variable or method `reset' for main:Object
rake command is to be run from the command line; not from within the rails console.
See Rails gives NameError for all command line methods such as generate or rake for the same mistake.
I receive the following error, even after doing a rake db:test:prepare. I am running rails 4.0.
1) Core::PostsController GET index assigns all posts as #posts
Failure/Error: post = Post.create! valid_attributes
ActiveRecord::StatementInvalid:
Could not find table 'core_posts'
# ./spec/controllers/core/posts_controller_spec.rb:36:in `block (3 levels) in <module:Core>'
I am running this test inside an engine, so could it be something related? My test looks like this:
module Core
describe PostsController do
# This should return the minimal set of attributes required to create a valid
# Post. As you add validations to Post, be sure to
# adjust the attributes here as well.
let(:valid_attributes) { { } }
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# PostsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET index" do
it "assigns all posts as #posts" do
post = Post.create! valid_attributes
get :index, {}, valid_session
assigns(:posts).should eq([post])
end
end
end
end
Any ideas? Thanks!
cd into the engine dir & generate a dummy app for testing for your engine:
rails plugin new . --full --mountable --dummy-path spec/dummy
the above command will generate a full mountable engine with isolated namespace, meaning that all the controllers and models from this engine will be isolated within the namespace of the engine. For instance, the Post model later will be called Core::Post, and not simply Post. Since you have already generated the app -- incase of conflicts, you can skip the change.
Further, engine comes with a dummy application, located at spec/dummy because we told it to do that with the --dummy_path option. This dummy application is just a bare-bones Rails application that can be used to test the engine as if it was mounted inside a real application.
Then, you need to change rspec to use this dummy app, by making following changes:
inside spec/spec_helper.rb change this line
require File.expand_path("../../config/environment", __FILE__)
to this
require File.expand_path("../dummy/config/environment",__FILE__)
As config/environment.rb file doesn’t live two directories up, but rather inside spec/dummy.
Now, you can run the migration by following command.
RAILS_ENV=test bin/rake db:migrate
Why not db:test:prepare?
We cannot run rake db:test:prepare because it is unavailable. This
db:migrate task is especially altered for engines, and will run the migrations for the engine PLUS migrations within the spec/dummy/db folder.
Try to recreate your test DB
without rbenv
RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:test:prepare
with rbenv
RAILS_ENV=test bundle exec rake db:drop
RAILS_ENV=test bundle exec rake db:create
RAILS_ENV=test bundle exec rake db:test:prepare
When I try to run my spec, I get an uninitialized constant error. My spec looks like this:
describe Facility do
it { should have_many(:units) }
it { should have_many(:facilities_users) }
it { should have_many(:administrators) }
it { should have_many(:facility_employees) }
end
The error is:
facility_spec.rb:1:in `<top (required)>': uninitialized constant Facility (NameError)
I certainly have a Facility model, so I'm not sure why this would happen.
You should try running rake spec instead of rspec spec.
But both may work.
If not working try Try bundle exec rspec spec or bundle exec rake spec.
Source: When trying to run rspec I get uninitialized constant.
Add the following at the top of your file:
require 'spec_helper'
If you are using the 'rspec-rails' gem, then run
rails g rspec:install
This will create the spec/spec_helper.rb file (you should edit it if you're not using ActiveRecord so it runs you spec setup correctly).
After that, ensure you are requiring the helper at the top of your spec files:
require 'spec_helper'
If this didn't work for you, there might be more issues like:
You're trying to test a file under the lib/ directory. In this case,
make sure this file is loaded with the environment
(config/application.rb -> autoload_paths) or require it explicitly.
The constant actually doesn't exist. It could be inside a namespace or just a typo.
In the spec file, require the file where Facility class is defined.