Rspec - stub module method
shared_examples_for SomeModule do
let(:instance) { described_class.new }
it 'does something exciting' do
instance.should_receive(:method_two).and_return('MANUAL')
expect(instance.method_one).to eq(some_value)
end
end
describe SomeController do
include_examples SomeModule
end
Rspec how to stub module instance method?
You are stubbing wrongly. Your A::B
is a module, so you don´t have instances, instances are of classes. You forget the question mark also.
Try this to stub your module static method:
A::B.stub(:enabled?).and_return true
And in the second example (if you need) try this:
YYY::C.any_instance.stub(:a_method).and_return something
But I think you are trying to stub the enabled?
method in the class YYY::C
, so you need to use this:
YYY::C.any_instance.stub(:enabled?).and_return true
Then when calling :a_method, enabled?
will return true.
How to stub a method from a module in a controller?
Well, it's the controller that will receive the message. The test, as a client, does not care about how the method is defined, "normally" or mixed-in.
If it were a controller spec, you'd be able to do this:
allow(controller).to receive(:send_get_request).with(...)
As for the feature specs, don't stub anything in there. It's supposed to be the "real" interaction. In feature specs you use capybara (or something) to fill the forms and then check the database (to see if a user got created, etc.)
To shield your feature specs from external api, you can use VCR gem. It basically runs your external query one time, writes the response into a file and then, on subsequent runs, just plays it back, without contacting external api again.
How to mock Ruby Module functions?
Given the module you describe in your question
module ModuleA ; end
module ModuleA::ModuleB
def self.my_function( arg )
end
end
and the function under test, which calls the module function
def foo(arg)
ModuleA::ModuleB.my_function(arg)
end
then you can test that foo
calls myfunction
like this:
describe :foo do
it "should delegate to myfunction" do
arg = mock 'arg'
result = mock 'result'
ModuleA::ModuleB.should_receive(:my_function).with(arg).and_return(result)
foo(arg).should == result
end
end
How to test module in a module with rspec?
First of all, module A do
syntax seems to be wrong - there shouldn't be a do
word. The second thing is that you tries to call foo method on a module A::B
(there is no class method defined on A::B
- foo is an instance method). Your code should looks like:
module A
module B
def self.foo
end
end
end
*(In this case, you can call A::B.foo
)
OR if you want to have foo as an instance method:
module A
module B
def foo
end
end
end
In this case you cannot call A::B.foo
, but you can create a class, which can include A::B
module:
class Test
include A::B
end
Now you can call Test.new.foo
Regarding rspec testing:
- class method: as you described
- instance method:
- define Test class in your spec and then test this class
- include this module to your rspec context (ugly way)
Related Topics
Paperclip File Not Found Error
How to Delete a Range of Values from an Array
How to Make Xcode Use the Correct Version of Ruby When Running a Script
Split Seeds.Rb into Multiple Sections
How to Use Regex for Utf8 in Ruby
Ssl_Connect Syscall Returned=5 Errno=0 State=Sslv2/V3 Read Server Hello A
Ruby: Yield Block from a Block
Create a Human-Readable List with "And" Inserted Before the Last Element from a Ruby List
Openssl Trouble with Ruby 1.9.3
How to Integrate 'Premailer' with Rails
Ruby on Rails Map.Root Doesn't Seem to Be Working
Catching Timeout Errors with Ruby Mechanize
Spacing Around Parentheses in Ruby
Parsing Large Xml Files W/ Ruby & Nokogiri
Ruby Errors with Os X Yosemite