I am using Rails 3.1, RSpec 2.6, and rr 1.0.4 and I get a NoMethodError:
undefined method `stub' for #<Activity:0x007faa90996190>
I am trying to utilize the RSpec test below to test my "Activities" show.haml view. If I change my spec_helper.rb file to use RSpec for mocks instead of rr then the test passes. I have tried changing the syntax of the code but to no success.
I found several websites stating that RSpec and rr do not "play well together and one person provided this rpsec-rr solution which did not work for me.
This is my show.haml_spec.rb
require 'spec_helper'
describe "activities/show.haml" do
before(:each) do
#activity = assign(:activity, stub_model(Activity))
end
it "renders attributes in .haml" do
render
end
end
This is the output from my Eclipse compiler using Aptana Studio
Failures:
1) activities/show.haml renders attributes in .haml
Failure/Error: #activity = assign(:activity, stub_model(Activity))
NoMethodError:
undefined method `stub' for #<Activity:0x007faa90996190>
# ./spec/views/activities/show.haml_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 0.15479 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/views/activities/show.haml_spec.rb:8 # activities/show.haml renders attributes in .haml
Any recommendation to an alternate syntax would be greatly appreciated!!
Note that as of RR 1.0.5 (released 3/28/2013) this problem should no longer be present -- RR is fully compatible with RSpec 2.
Related
When I run rspec with an example that will pass, everything is fine.
But when the expectation doesn't match I got a weird TypeError instead of a useful message about the difference between expected and actual value.
Given
ruby 2.3.1p112
rspec 3.5.2
a spec helper just as being generated by rspec —init
When
describe "Basics" do
it "runs rspec successfully" do
x = true
expect(x).to be(true)
end
end
Then everything is fine
Finished in 0.00286 seconds (files took 0.09671 seconds to load)
1 example, 0 failures
BUT when I change x to false, thus the match fails
When
describe "Basics" do
it "runs rspec successfully" do
x = false
expect(x).to be(true)
end
end
Then
Failures:
1) Basics runs rspec successfully
Failure/Error: expect(x).to be(true)
TypeError: <———
superclass must be a Class (Module given)
# ./spec/lib/basic_spec.rb:4:in `block (2 levels) in <top (required)>'
Finished in 0.06168 seconds (files took 0.09715 seconds to load)
1 example, 1 failure
I would expect an output like
lhs: false
rhs: true
instead of this TypeError.
What's wrong here?
the reason for this was the following line in my gem file
gem "prettyprint"
I removed it from Gemfile and everything works as expected.
As I figured out, it's possible to use pp some_object without using any gem.
I am following a tutorial and have been getting some errors, I believe some of the language is outdated with Rspec and have messed around alot to try and fix this but have not been able to.
The error i get is
NoMethodError:
undefined method `should_recieve' for #<Panda:0x007f9cd45c6458>
# ./spec/zookeeper_spec.rb:9:in `block (2 levels) in <top (required)>'
The code in question is
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
panda.should_recieve(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end
I have realised that should_recieve should actually be should_receive and this has resolved the problem. I then replaced should_receive with and expectation and to receive
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
expect(panda).to receive(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end
I am running through the TestFirst tutorial and can't get past the first challenge due to some troubles with Rake (and rspec in general).
The tests I am trying to run:
require 'hello'
describe "the hello function" do
it "says hello" do
hello.should == "Hello!"
end
end
describe "the greet function" do
it "says hello to someone" do
greet("Alice").should == "Hello, Alice!"
end
it "says hello to someone else" do
greet("Bob").should == "Hello, Bob!"
end
end
I've only gotten so far as to create the hello.rb file (according to the tutorial) and adding
def hello
end
For the record, before I created the file, I got the long output saying that the require "hello" wasn't working, as expected, and upon creating the file that went away. So that all worked fine.
When I run the rake command, my output is (the bolded part is my main concern):
wendy#wendy-EL1352:~/the_odin_project/learn_ruby/00_hello$ rake
(in /home/wendy/the_odin_project/learn_ruby)
the hello function
says hello (FAILED - 1)
Failures:
1) the hello function says hello
Failure/Error: hello.should == "Hello!"
expected: "Hello!"
got: nil (using ==)
# ./00_hello/hello_spec.rb:120:in `block (2 levels) in '
Deprecation Warnings:
Using should from rspec-expectations' old :should syntax without explicitly enabling >the syntax is deprecated. Use the new :expect syntax or explicitly enable :should >instead. Called from /home/wendy/the_odin_project/learn_ruby/00_hello/hello_spec.rb:120:in >`block (2 levels) in '.
If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
config.raise_errors_for_deprecations!, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
1 deprecation warning total
Finished in 0.00334 seconds (files took 0.14558 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./00_hello/hello_spec.rb:119 # the hello function says hello
rake aborted!
/home/wendy/.rvm/rubies/ruby-2.0.0-p481/bin/ruby -S rspec /home/wendy/the_odin_project/learn_ruby/00_hello/hello_spec.rb -I/home/wendy/the_odin_project/learn_ruby/00_hello -I/home/wendy/the_odin_project/learn_ruby/00_hello/solution -f documentation -r ./rspec_config failed
/home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/rake_task.rb:156:in run_task'
/home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/rake_task.rb:124:inblock (2 levels) in initialize'
/home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/rake_task.rb:122:in `block in initialize'
Tasks: TOP => default => spec
(See full trace by running task with --trace)
I'm not hugely concerned with the deprecation warning (I've looked through enough to be reasonably certain these aren't related). Although if you think that's the cause I'll bow my head in shame. I do need to figure out how to either rewrite the specs or change the config settings, but I have faith that I can do that myself. My real concern here is that Rake has access to the tests and can run them, but aborts before showing the error messages.
If I run rspec hello_spec.rb the output is here:
wendy#wendy-EL1352:~/the_odin_project/learn_ruby/00_hello$ rspec hello_spec.rb
/home/wendy/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in require': cannot load such file -- hello (LoadError)
from /home/wendy/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:inrequire'
from /home/wendy/the_odin_project/learn_ruby/00_hello/hello_spec.rb:116:in <top (required)>'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:inload'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in block in load_spec_files'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:ineach'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in load_spec_files'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:97:insetup'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:85:in run'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:70:inrun'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:38:in invoke'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/gems/rspec-core-3.0.2/exe/rspec:4:in'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/rspec:23:in load'
from /home/wendy/.rvm/gems/ruby-2.0.0-p481#railstutorial_rails_4_0/bin/rspec:23:in'
So I'm kind of wondering why running rake has no problem linking the required hello.rb file but the rspec command doesn't. I just want to get these tests to run (and fail) properly so I can get on with the task.
The rakefile:
# This Rakefile has all the right settings to run the tests inside each lab
gem 'rspec', '~>2'
require 'rspec/core/rake_task'
task :default => :spec
desc "run tests for this lab"
RSpec::Core::RakeTask.new do |task|
lab = Rake.application.original_dir
task.pattern = "#{lab}/*_spec.rb"
task.rspec_opts = [ "-I#{lab}", "-I#{lab}/solution", '-f documentation', '-r ./rspec_config']
task.verbose = false
end
Adding gem 'ruby gems' does nothing but add more confusion to the outputs.
I have initialized rspec with rspec --init in previous attempts at this, both in the 00_hello directory as well as in the learn_ruby directory. In this case, I run into exactly the same error messages.
I've been going at learning Ruby and Rails for the past few months. Once or twice I've almost asked a question here, but I've ended up finding my answer halfway through drafting it. I am not having that kind of luck this time. I am truly stumped. Please help.
Thank you.
Edit with more info on rspec --init and use of 'spec_helper'
in the 00_hello folder, rspec --init and then including the spec_helper in the hello_spec.rb (as shown above - I included require 'spec_helper' below the require 'hello') produces the following error when run with the rake command:
wendy#wendy-EL1352:~/the_odin_project/learn_ruby/00_hello$ rake (in
/home/wendy/the_odin_project/learn_ruby)
/home/wendy/.rvm/rubies/ruby-2.0.0-p481/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in
`require': cannot load such file -- spec_helper (LoadError)
In the learn_ruby directory, I get the same output as above, where the tests run well enough to fail, but then I see rake abort! before the errors are listed and the same error as before:
the hello function says hello (FAILED - 1)
Failures:
1) the hello function says hello
Failure/Error: hello.should == "Hello!"
NameError:
undefined local variable or method hello' for #<RSpec::ExampleGroups::TheHelloFunction:0x00000002989278>
# ./00_hello/hello_spec.rb:122:inblock (2 levels) in '
Finished in 0.00094 seconds (files took 0.16985 seconds to load) 1
example, 1 failure
Failed examples:
rspec ./00_hello/hello_spec.rb:121 # the hello function says hello
rake aborted! /home/wendy/.rvm/rubies/ruby-2.0.0-p481/bin/ruby -S
rspec /home/wendy/the_odin_project/learn_ruby/00_hello/hello_spec.rb
-I/home/wendy/the_odin_project/learn_ruby/00_hello -I/home/wendy/the_odin_project/learn_ruby/00_hello/solution -f documentation -r ./rspec_config failed
(the reason the test failure is different is because I've gone back to just having the hello.rb file created without inserting the class name into the file - the tests are half running, but the rake aborted! won't go away)
This guy:
-I/home/wendy/the_odin_project/learn_ruby/00_hello/solution -f documentation -r ./rspec_config failed
can be found in the Rakefile:
RSpec::Core::RakeTask.new do |task|
lab = Rake.application.original_dir
task.pattern = "#{lab}/*_spec.rb"
**task.rspec_opts = [ "-I#{lab}", "-I#{lab}/solution", '-f documentation', '-r ./rspec_config']**
task.verbose = false
end
But I do not have enough understanding of Rake to even parse apart Google search results for problems with that line, or find what search terms to use to find relevant information. :/
I am trying to run given scenarios as many times per each account type. I found out in other thread that "around hooks" could be the best option to do it the DRY way.
Below is my code:
require 'watir-webdriver'
require 'page-object/page_factory'
require 'page_navigation'
require 'data_magic'
require_relative 'pages/login_page'
Before do
beforeCodeBlock
...
end
Around('#hooks_test') do|scenario, block|
DataMagic.load('default.yml')
account_type = {"listener" => DataMagic.yml["listener_data"],
"free" => DataMagic.yml["free_data"],
"premium" => DataMagic.yml["premium_data"]}
account_type .each {|key, value|
puts "Running scenario: #{scenario.name} as #{key} user"
visit_page(LoginPage)
on_page(LoginPage).sign_in(value["username"],value["password"]) #Login as
block.call
}
end
After do
afterCodeBlock
...
end
So when I run the feature file I get the following output and subsequent error:
Running scenario: Hooks test as listener user
undefined method `visit_page' for nil:NilClass (NoMethodError)
/myProject/features/support/hooks.rb:38:in `block (2 levels) in <top (required)>'
/myProject/features/support/hooks.rb:35:in `each'
/myProject/features/support/hooks.rb:35:in `Around'
Everything seems to be working as expected until the execution deals with the visit_page method that belongs to PageObject gem.
There are possibly two problems I see here.
First of all you are getting the error because you did not register the PageObject::PageFactory module with World. Add this line after the requires:
World(PageObject::PageFactory)
Second, you are using an Around block and calling the same Scenario multiple times. From what I am seeing, you are doing this so that it will exercise the same behavior with different logged in users. If the behavior is exactly the same, what are you accomplishing by running it three times?
I have some Ruby code I need to execute in the Redmine Ruby app in order to enable a module in all projects at once.
Surprisingly, ruby does access the Projects, but raises a NoMethodError when accessing a few particular methods inside each of the "Project" objects.
Here is the code:
Project.find(:all).each do |project|
print "Enabling modules for project '#{project.identifier}' ... "
puts project.methods.sort # this does print "enabled_module_names"
puts project.enabled_module_names
end
This fails with:
hostname:/srv/apps/redmine# script/runner vendor/plugins/customplugin/lib/enable_modules.rb
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in `method_missing': undefined method `enabled_module_names' for #<Project:0x7f28985c1cb0> (NoMethodError)
from vendor/plugins/customplugin/lib/enable_modules.rb:14
from vendor/plugins/customplugin/lib/enable_modules.rb:7:in `each'
from vendor/plugins/customplugin/lib/enable_modules.rb:7
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `eval'
from /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:46
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/runner:3
I have scratched my head a lot, but I can't grasp why would the code find the "Project" symbol but not the methods within, especially as "project.methods" does indeed list "enabled_module_names".
Any help most welcome.
Are you sure that enable_module_names is an instance method?
Is it within Project.instance_methods?
Edit (summary of the comments below):
In earlier versions, you have to use following:
enabled_module_names = project.enabled_modules.collect(&:name)
The getter is present in later versions only (see rev.4460 for details of this change)