Assigning the rspec mocking object property

I have an rspec mocked object, the value is assigned to the is property. I try my best to have this expectation meet in my rspec testing. Just wondering what sitax 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 

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 that I get 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 to set the expected purpose of an object?

thanks

+4
source share
2 answers

There is no such thing as property appropriation in Ruby. In Ruby, this is all a method call. So, you are mocking the setter method, like any other method:

 campaign_model.should_receive(:creationDate=).with(...) 

BTW: diagnostic messages that print tests are not just for shpw. 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 sent already tells you what method name you need to find:

  unexpected message :creationDate= with ("2010/5/7") ^^^^^^^^^^^^^^ 
+12
source

Found links about it here

It just add: creationDate =, not just: createDate pending.

0
source

Source: https://habr.com/ru/post/1309089/


All Articles