I have a rspec mocked object, a value is assign to is property. I am struggleing to have that expectation met in my rspec test. Just wondering what the sytax is? The code:
def create
@new_campaign = AdCampaign.new(params[:new_campaign])
@new_campaign.creationDate = "#{Time.now.year}/#{Time.now.mon}/#{Time.now.day}"
if @new_campaign.save
flash[:status] = "Success"
else
flash[:status] = "Failed"
end end
The test
it "should able to create new campaign when form is submitted" do
campaign_model = mock_model(AdCampaign)
AdCampaign.should_receive(:new).with(params[:new_campaign]).and_return(campaign_model)
campaign_model.should_receive(:creationDate).with("#{Time.now.year}/#{Time.now.mon}/#{Time.now.day}")campaign_model.should_receive(:save).and_return(true)
post :create
flash[:status].should == 'Success'
response.should render_template('create') end
The problem is I am getting this error:
Spec::Mocks::MockExpectationError in 'CampaignController new campaigns should able to create new campaign when form is submitted' Mock "AdCampaign_1002" received unexpected message :creationDate= with ("2010/5/7")
So how do i set a expectation for object property assignment?
Thanks
Just check for the return value. If the method is working with external objects & sending orders to them, then you can mock the interactions with these objects. If the method is REQUESTING data from an external service (like an API), then you can use a stub to provide this data for testing purposes.
Stub: A class or object that implements the methods of the class/object to be faked and returns always what you want. Mock: The same of stub, but it adds some logic that "verifies" when a method is called so you can be sure some implementation is calling that method.
RSpec features doubles that can be used as 'stand-ins' to mock an object that's being used by another object. Doubles are useful when testing the behaviour and interaction between objects when we don't want to call the real objects - something that can take time and often has dependencies we're not concerned with.
In RSpec, a stub is often called a Method Stub, it's a special type of method that “stands in” for an existing method, or for a method that doesn't even exist yet.
There is no such thing as "property assignment" in Ruby. In Ruby, everything is a method call. So, you mock the setter method just like you would any other method:
campaign_model.should_receive(:creationDate=).with(...)
BTW: the diagnostic messages that the tests print out are not just for show. In this case, the message already tells you everything you need to know:
Spec::Mocks::MockExpectationError in 'CampaignController new campaigns should able to create new campaign when form is submitted' Mock "AdCampaign_1002" received unexpected message :creationDate= with ("2010/5/7")
As you can see, the message you posted already tells you what the name of the method is you need mock right there:
unexpected message :creationDate= with ("2010/5/7")
^^^^^^^^^^^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With