I'm trying this:
require 'minitest'
Minitest::Assertions::assert_equal(1, 1)
Doesn't work:
NoMethodError: undefined method `assert_equal' for Minitest::Assertions:Module
What is the right way? The method does exist.
module Assertions
extend Minitest::Assertions
class << self
attr_accessor :assertions
end
self.assertions = 0
end
Assertions.assert(true)
Assertions.assert(false)
The Minitest::Assertions module expects to be able to increment an instance accessor named assertions.
http://docs.seattlerb.org/minitest/Minitest/Assertions.html
Related
I'm trying to use Ruby assertion functions, and I keep getting the same error:
undefined method `add_assertion' for nil:NilClass (NoMethodError)
#!/usr/bin/env ruby
require 'test/unit'
require_relative 'functions'
class TestObj < Test::Unit::TestCase
def initialize()
end
def test_me(obj)
for i in 0..2
assert_equal('ok', obj.a_function(input1, input2))
end
end
end
function_session = my_object.new()
test_session = TestValuePair.new()
test_session.test_me(function_session)
I've already tried including the assertion library like so:
require "test/unit/assertions"
include Test::Unit::Assertions
Neither of my objects take any arguments to be initialized. I also know that the my_object object does work.
Anyone have any insight?
So why is this happening? It has to be a namespace error, I just don't understand where it is. I add a method to Fixnum like so in a file file.rb
module M
class Fixnum
def foo
return true
end
end
end
then I'll make a test like so:
require 'minitest/autorun'
require './file.rb' #the path is correct
class SomeTest < MiniTest::Test
def test_foo
assert 3.foo
end
end
which will in turn throw a
NoMethodError: undefined method `foo' for 3:Fixnum
when I run the test, and I am left scratching my head - even if I include M to include the module (applying the namespace?) for the test it still throws the error. I can use custom classes just fine, it's only when I try to add a method to an existing "open class".
Yes, you have defined your own M::Fixnum class which actually has nothing to do with ::Fixnum in the global namespace. The following will solve an issue:
module M
class ::Fixnum
def foo
return true
end
end
end
5.foo
#⇒ true
Please note, in the code above module M has no sense, since the code nevertheless monkey-patches the global Fixnum. The code is here just to show how you would monkey-patch the global class from inside another module code.
Plus, Ruby2 introduced refinements, which are likely what you are intended to use.
I'm trying to access a method from a module in one of my spec helpers
I include the module in the test helper
module Support
class RestHelper
include Rest::Rest
def create_rest_client_for_ifa
# Call method from module
create_rest_client(uname, pword)
end
end
end
But I keep getting a NoMethodError when I run my spec:
Failure/Error: #rest_client = Support::RestHelper.create_rest_client_for_ifa
NoMethodError:
undefined method `create_rest_client' for Support::RestHelper:Class
Here is my module code:
module Rest
module Rest
.
.
def create_rest_client(uname, pword)
# code
end
.
.
end
end
It seems to work fine when I test it in the rails console
$ RAILS_ENV=test rails c
irb> include Rest::Rest
=> Object
irb> create_rest_client(uname, pword)
What am I missing? Why can't I access the method from the test helper?
Any help will be much appreciated.
As I remember, include adds module methods as instance methods, extend adds them as class methods.
What I want to achieve is something like below, i.e. calling a base class method from extended modules method:
class BaseClass
def behavior
puts 'base class behavior'
end
end
module ChildModule
def behavior
super.behavior
puts 'child module behavior'
end
end
o = BaseClass.new
o.extend ChildModule
o.behavior
and it outputs as follows (with ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux])
base class behavior
t.rb:9:in `behavior': undefined method `behavior' for nil:NilClass (NoMethodError)
from t.rb:16:in `<main>'
My guess is that I can not use super, as super does not exist in the module. But it prints out that line from the super method, is that strange?
How do I achieve it what I want above?
Answer by #davidrac is working, however being more curious, would like to know, how can I get a handle to base class instance? Say for instance I added the following method to BaseClass
def behavior2
puts 'base class behavior2'
end
and overrides it in ChildModule. Now from ChildModule behavior can I make a call to behavior2 of BaseModule?
I think the correct syntax is:
module ChildModule
def behavior
super
puts 'child module behavior'
end
end
I am trying to test a single method in ruby. It is in a separate file so basically:
a.rb:
def my_method
...
end
in my a_spec.rb
require 'minitest/autorun'
Object.instance_eval do
load("path_to/a.rb")
def hello_world
...
end
end
When I try to run my test, it says that my_method is a private method while I can actually call Object.hello_world outright. What gives?
Also, is there an easier way to test plain ruby methods(no classes or modules) with minitest?
Doing the load above doesn't add the methods of a.rb as singleton methods to Object. Rather, it adds the methods to the global namespace. (The fact that you are doing the load inside the block where self refers to the Object class is irrelevant.)
With you above code, you should be able to call *my_method* directly in your tests:
class MyTest < MiniTest::Unit::TestCase
def test_my_method
assert my_method
end
def test_hello_world
assert Object.hello_world
end
end