lib/rspec/core/my_methods.rb
module MyLovelyModule
class My_methods
def my_lovely_method
save_world
config.to_prepare do
Dir.glob(Rails.root + "../simpleRspec/dummy_classes.rb").each do |c|
require_dependency(c)
end
end
end
end
end
lib/simpleRspec/dummy_classes.rb
describe MyLovelyModule do
class DummyClasses
before(:all) do
#dummy = DummyClasses.new
#dummy.extend MyLovelyModule
end
describe "MyLovelyModule" do
it "saves the world" do
expect {
#dummy.my_lovely_method
}.to raise_error MeltDownException
end
end
end
end
I got a error like that:-
C:/Users/afzala/RubymineProjects/simpleRspec/lib/simpleRspec/dummy_classes.rb:2:in `<top (required)>': uninitialized constant MyLovelyModule (NameError)
from -e:1:in `load'
from -e:1:in `<main>'
Process finished with exit code 1
could anyone please help me out for this issue
You need to load your module first. You also need to close the class before you define tests:
require 'lib/rspec/core/my_methods.rb'
describe MyLovelyModule do
class DummyClasses
end
before(:all) do
#dummy = DummyClasses.new
#dummy.extend MyLovelyModule
end
describe "MyLovelyModule" do
it "saves the world" do
expect {
#dummy.my_lovely_method
}.to raise_error MeltDownException
end
end
end
Also you will not be able to access #dummy in your test. You need to use before(:each) in place of before(:all)
Related
I want to check whether the block is called in my function using rspec. Below is my code:
class SP
def speak(options={},&block)
puts "speak called"
block.call()
rescue ZeroDivisionError => e
end
end
describe SP do
it "testing speak functionality can receive a block" do
sp = SP.new
def test_func
a = 1
end
sp_mock = double(sp)
expect(sp_mock).to receive(:speak).with(test_func)
sp.speak(test_func)
end
end
Below is my error:
SP testing speak functionality can receive a block
Failure/Error: block.call()
NoMethodError:
undefined method `call' for nil:NilClass
# ./test.rb:9:in `speak'
# ./test.rb:25:in `block (2 levels) in <top (required)>'
Could you please help. I spent lots of time in that.
You have to use one of RSpec's yield matcher:
describe SP do
it "testing speak functionality can receive a block" do
sp = SP.new
expect { |b| sp.speak(&b) }.to yield_control
end
end
I think Stefan provided the best answer. However I wanted to point out that you should be testing the behaviour of the code instead of implementation details.
describe SP do
it "testing speak functionality can receive a block" do
sp = SP.new
called = false
test_func = -> () { called = true }
sp.speak(&test_func)
expect(called).to eql(true)
end
end
I want to test the functionality of the method using rspec that receives anonymous block and not raise error. Below is my code:
class SP
def speak(options={},&block)
puts "speak called"
block.call()
rescue StandardError => e
puts e.inspect()
end
end
describe SP do
it "testing speak functionality not to raise error" do
sp = SP.new
sp_mock = double(sp)
expect(sp_mock).to receive(:speak).with(sp.speak{raise StandardError}).not_to raise_error
end
end
It is below throwing error
SP testing speak functionality not to raise error
Failure/Error: expect(sp).to receive(:speak).with(sp.speak{raise StandardError})
(#<SP:0x007fead2081d20>).speak(nil)
expected: 1 time with arguments: (nil)
received: 0 times
# ./test.rb:22:in `block (2 levels) in <top (required)>'
Spent a lot of time browsing articles of ruby blocks and ruby documentation but can't figure out.
It's too complicated for no reason. Did you mean this?
it "testing speak functionality not to raise error" do
sp = SP.new
expect {
sp.speak {raise StandardError}
}.to_not raise_error
end
I have the following test code:
require_relative '../spec_helper'
describe Chess::King do
before do
#piece = Chess::King.new('king1',:black)
#board = Chess::Board.new
end
describe '#possible_moves' do
context "placing king at location 4,5" do
#board.grid[4][5] = #piece
subject {#piece.possible_moves(#board)}
it {is_expected.to eq([3,5],[3,6],[4,6],[5,6],[5,5])}
end
end
end
Why am I getting this error:
in block (3 levels) in <top (required)>': undefined methodgrid' for nil:NilClass (NoMethodError)
I am not sure about this line: #board.grid[4][5] = #piece.
My intention here is to assign the piece object to the grid instance variable of board (8x8 array).
Try using let instead:
require_relative '../spec_helper'
describe Chess::King do
let(:piece) { Chess::King.new('king1',:black) }
let(:board) { Chess::Board.new }
describe '#possible_moves' do
context "placing king at location 4,5" do
before(:each) { board.grid[4][5] = piece }
subject {piece.possible_moves(board)}
it {is_expected.to eq([3,5],[3,6],[4,6],[5,6],[5,5])}
end
end
end
Given the following code:
module Backup
module Destination
class Base
def initialize
puts 'Base'
end
end
end
end
module Backup
module Destination
class Test < Base
def initialize
puts 'Test'
super
end
end
end
end
Backup::Destination::Test.new
This works as expected, outputting:
Test
Base
However if I split things up like this:
# lib/backup.rb
require_relative 'backup/destination/base'
module Backup; end
# lib/backup/destination/base.rb
require_relative 'test'
module Backup
module Destination
class Base
def initialize
puts 'Base'
end
end
end
end
# lib/backup/destination/test.rb
module Backup
module Destination
class Test < Base
def initialize
puts 'Test'
super
end
end
end
end
And execute with the following (from irb):
require_relative 'lib/backup'
I get this error:
NameError: uninitialized constant Backup::Destination::Base
from /lib/backup/destination/test.rb:3:in `<module:Destination>'
from /lib/backup/destination/test.rb:2:in `<module:Backup>'
from /lib/backup/destination/test.rb:1:in `<top (required)>'
from /lib/backup/destination/base.rb:1:in `require_relative'
from /lib/backup/destination/base.rb:1:in `<top (required)>'
from /lib/backup.rb:1:in `require_relative'
from /lib/backup.rb:1:in `<top (required)>'
from (irb):1:in `require_relative'
What am I missing?
Note: I couldn't post the above without adding more details. Stupid feature because in this case code is worth a thousand words. (this text allowed the question to be posted)
The problem is that you are requiring test.rb before your Base class is defined. One possible solution is to move your require to the bottom of base.rb.
Another possible solution is to remove your require from base and require both files in the correct order from backup.
Made the following changes to fix the problem:
# lib/backup.rb
require_relative 'backup/destination/base'
require_relative 'backup/destination/test'
module Backup; end
And removed the require_relative statement from lib/backup/destination/base.rb. This fixed the order of the require_relative statements. I mistakenly thought the files were required before anything was executed.
In Rspec, testing whether an instance is able to call method x.
DockingStation.rb
class DockingStation
def release_bike
end
end
Docking_spec.rb
require_relative '../lib/DockingStation'
describe DockingStation do
before(:each) do
#dockstat = DockingStation.new
end
describe "#DockingStation" do
it "Check release method" do
expect(#dockstat).to respond_to(:release_bike)
end
end
end
Currently getting the following error message:
1) DockingStation#DockingStation Check release method
Failure/Error: expect(#dockstat).to respond_to(:release_bike)
expected #<DockingStation:0x007fa518a6da00> to respond to :release_bike
# ./spec/Docking_spec.rb:10:in `block (3 levels) in <top (required)>'
What I'm expecting is for the object #dockstat instantiated in the Docking_spec.rb to respond to the release_bike method defined in DockingStation.rb, but this is not the case.
require_relative '../DockingStation'