I'm trying to put the rspec for active link condition present in the application_helper.rb.
Application_helper.rb code:
def active_class(link_path)
current_page?(link_path) ? 'active' : ''
end
i tried to put the rspec for this active_class method, i used stub for this purpose.This is my rspec code for active_class method.
Application_helper_spec.rb code:
describe 'when called from "index" action' do
before
helper.stub!(:action_name).and_return('index')
end
it 'should do' do
helper.active_class.should == 'active'
end
describe 'when called from "other" action' do
before
helper.stub!(:action_name).and_return('other')
end
it 'should do' do
helper.active_class.should == 'empty'
end
I'm getting the error as undefined method stub.How can i get overcome from this issue?
I use slightly different interface for stubbing and mocking which looks like this:
allow(helper).to receive(:action_name).and_return('index')
When you just want to stub the response.
But if you need to set up an expectation:
expect(helper).to receive(:action_name).and_return('index')
You can find more info in the docs: https://relishapp.com/rspec/rspec-mocks/v/3-7/docs/basics
Related
This is very weird. The following code:
describe "Spike" do
before(:all) do
something = double('name')
end
describe "a test" do
it "is basic" do
1.should == 1
end
end
end
Fails with:
NoMethodError: undefined method `double' for #<RSpec::Core::ExampleGroup::Nested_1:0x9dec5e8 #__memoized=nil>
./spec/unit/whatever/spike_spec.rb:3:in `block (2 levels) in '
Change the before(:all) to before(:each) and everything is fine. I'm using Ruby 1.9.3
Any ideas?
This is expected behavior, since doubles get cleaned out after every example. You should stick with using these in a before(:each) block.
See https://www.relishapp.com/rspec/rspec-mocks/docs/scope
Also see https://github.com/rspec/rspec-core/issues/202 for discussion on this.
Why am getting this error? How to fix?
1) User should exist
Failure/Error: User.should be_valid
NoMethodError:
undefined method `valid?' for #<Class:0x94b626c>
Test is:
require 'spec_helper'
describe User do
it "should exist" do
User.should be_valid
end
it "should not allow me to create a new user without required fields" do
User.new(:email => 'bob').should_not be_valid
end
end
The second test works ok, how can I get the first one to pass? I just want it to check that the model exists
Testing a class implicitly tests that it exists. Both code samples will error out if the class doesn't exist. The first is unnecessary.
Replace User.should be_valid with User.new.should be_valid in the first test. RSpec is calling valid? on the User class instead of an instance of it.
Below is the controller code in rspec for a master item.
To be very frank I'm very new to Ruby with a little knowledge of coding.
require 'spec_helper'
describe PayrollItemsController , "with valid params" do
before(:each) do
#payroll_item = mock_model(PayrollItem, :update_attributes => true)
PayrollItem.stub!(:find).with("1").and_return(#payroll_item)
end
it "should find PayrollItem and return object" do
PayrollItem.should_receive(:find).with("0").and_return(#payroll_item)
end
it "should update the PayrollItem object's attributes" do
#payroll_item.should_receive(:update_attributes).and_return(true)
end
end
When I run the controller code, following error displayed:
(Mock "PayrollItem_1001").update_attributes(any args)
expected: 1 time
received: 0 times
./payroll_items_controller_spec.rb:18:in `block (2 levels) in '
You have to actually make a request (get, post, put etc.) to the controller in order for the mock to have anything to check.
So for example:
it "should find PayrollItem and return object" do
PayrollItem.should_receive(:find).with("0").and_return(#payroll_item)
put :update, :id => "0"
end
In addition to that, looking at your code, you have some inconsistencies with your return values: in your before block you're stubbing PayrollItem.find with an id of 1 to return something, and then in your first spec you're mocking it with an id of 0 to return the same thing.
It's fine to both stub and mock the same method because they fulfill different functions: a stub makes sure that the code runs smoothly, while the mock actually checks an expectation. However, you should be stubbing/mocking it for the same argument, so that all the specs using this before block are testing the same thing.
describe SomeThing do
before :all do
# ...
FooClass.stub(:fooMethod).with('a').and_return("something")
end
end
Worked cool with rspec 1.
I've updated to rspec 2, and this is what i'm receiving for this line now:
Failure/Error:
FooClass.stub(:fooMethod).with('a').and_return("something")
NoMethodError:
undefined method `stub' for FooClass::Class
The rspec api but says: Person.stub(:find) { person }
What am i missing?
Is this inside an it or before block?
Stubs are not supported in before :all blocks. Stubs and mocks get cleared after each example. You can read more about this here. Change the before :all do to before do and this should work.
Consider the following two trivial models:
class Iq
def score
#Some Irrelevant Code
end
end
class Person
def iq_score
Iq.new(self).score #error here
end
end
And the following Rspec test:
describe "#iq_score" do
let(:person) { Person.new }
it "creates an instance of Iq with the person" do
Iq.should_receive(:new).with(person)
Iq.any_instance.stub(:score).and_return(100.0)
person.iq_score
end
end
When I run this test (or, rather, an analogous one), it appears the stub has not worked:
Failure/Error: person.iq_score
NoMethodError:
undefined method `iq_score' for nil:NilClass
The failure, as you might guess, is on the line marked "error here" above. When the should_receive line is commented out, this error disappears. What's going on?
Since RSpec has extended stubber functionality, now following way is correct:
Iq.should_receive(:new).with(person).and_call_original
It will (1) check expectation (2) return control to original function, not just return nil.
You're stubbing away the initializer:
Iq.should_receive(:new).with(person)
returns nil, so Iq.new is nil. To fix, just do this:
Iq.should_receive(:new).with(person).and_return(mock('iq', :iq_score => 34))
person.iq_score.should == 34 // assert it is really the mock you get