Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 770247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:28:06+00:00 2026-05-14T18:28:06+00:00

I have a rspec mocked object, a value is assign to is property. I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:28:07+00:00Added an answer on May 14, 2026 at 6:28 pm

    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")
                        ^^^^^^^^^^^^^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just installed Rspec and Rspec-rails. When i try to run the test,
My homecontroller has: def about() end And I have a rspec test that does
I have a rspec test that contains about 20 ExampleGroups(defined by the 'describe' keyword).
In rspec (1.2.9), what is the correct way to specify that an object will
I have a bunch of rspec tests that are taking entirely too long to
I have a rails 2.3.8 app with rspec tests (not written by me, just
My rspec title test is failing with: 1) HomeController Get 'index should have the
I have a ruby script I'm trying to test with rspec. Is there a
I have a nested controller that works fine, and I'd like to setup rspec
i have created an rspec test like : it should redirect to '/tavern' with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.