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 9077201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:17:08+00:00 2026-06-16T19:17:08+00:00

I started out learning how to mock things because using Factory Girl just isn’t

  • 0

I started out learning how to mock things because using Factory Girl just isn’t very practical for projects with 1000+ tests. I can’t be hitting the database for every test, especially if I hope to do any sort of automated continuous integration.

My Test:

    it "should return an URL with the desired security protocol" do
        p = Proposal.new
        p.expects(:status).returns(Proposal::PUBLISHED) #this should be invoked by public_url?
        p.expects(:public_url).returns("https://something")
        p.expects(:public_url).with(true).returns("https://something")
        p.expects(:public_url).with(false).returns("http://something")
        assert p.public_url.index("https") != nil
        assert p.public_url(true).index("https") != nil
        assert p.public_url(false).index("https") == nil
    end

The method for the above test:

def public_url(https = true)
    url = ""
    if self.published?
        # attepmt to find sluggable id
        id = sluggable_id
        url = (https ? "#{ENV['SSL_PROTOCOL']}://" : "http://") + 
            self.account.full_domain + "/view/" + id.to_s
    end

    return url
end 

def published?
    return self.status > UNPUBLISHED
end

But this is what I get when I run the test:

unsatisfied expectations:
- expected exactly once, not yet invoked: #<Proposal:0x7fbd07e82a30>.status(any_parameters)

Shouldn’t the call to public_url invoke status()?

If not, then if I have to call p.status myself, doesn’t that mean that p.public_url() is completetly ignoring the logic I have written, and strictly following what I have defined in expects?
How does that help with unit testing?
Maybe I don’t understand the purpose of mocking.


UPDATE:

I changed my test to this, as per @Philip’s suggestion, removing the need for any ActiveRecord shenanigans:

    it "should return an URL with the desired security protocol" do
        p = Proposal.new
        p.expects(:id).returns(1)
        p.expects(:status).returns(Proposal::PUBLISHED)
        p.expects(:account).returns(Account.new.stubs(:localhost))

        assert p.public_url.starts_with("https")
        assert p.public_url(true).starts_with("https")
        assert !p.public_url(false).starts_with("https")
    end

I guess now my question is, how do I stub out an account, using a fixture (that I have named localhost?). I get an error: undefined method 'full_domain' for Mocha::Expectation: but my fixture is defined as follows:

localhost:
  id: 1
  name: My first account
  full_domain: test.domain.vhost

I set up fixtures such that I could have the very basics of commonly accessed models for easy use throughout all of my tests. The Standard Model/Relationship for EVERY test (if I were do the testing by hand/with factory girl with no mocking, would require this:
Account, Subscription (has Account, SubscriptionPlan), SubscriptionPlan, User (belongs to Account), AccountProperties (has Account), and then the actual Object I’m testing on, which belongs to an Account and a User on that Account, which is a bit much for every test. haha


UPDATE 2:

Got it working:

it "should return an URL with the desired security protocol" do
            p = Proposal.new({:status => Proposal::PUBLISHED})
            p.expects(:id).returns(1)
            p.expects(:account).returns(accounts(:localhost)).times(3)

            assert p.public_url.starts_with?("https")
            assert p.public_url(true).starts_with?("https")
            assert !p.public_url(false).starts_with?("https")
        end

turns out, you access fixtures like, accounts or users.

  • 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-06-16T19:17:13+00:00Added an answer on June 16, 2026 at 7:17 pm

    My understanding of it is that your three p.expects(:public_url)... lines are stubbing out that method and returning the values you’ve told it to return. So once you’ve done that you’re method body is never being called… the stub has taken over. Hence… since you told it to expect a call to status as well, that is never going to happen.

    Looking at your code… I think what you want to stub out is self.account.full_domain and possibly id.to_s in this line:

    url = (https ? "#{ENV['SSL_PROTOCOL']}://" : "http://") + 
                self.account.full_domain + "/view/" + id.to_s
    

    That will skip any AR relation stuff, but still exercise the rest of the conditions in your method.

    Also.. I would change your assertions. The below will also match a non secure url like “http://https.com/blahblah” which isn’t what you really want. The other ones could also pass, but for invalid reasons as well.

    assert p.public_url(true).index("https") != nil
    

    So maybe something like:

    assert p.public_url(true).starts_with("https://") true
    

    Or, take the result and parse it using URI and then test the scheme directly (probably overkill thought)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started out learning iOS development. I'm using some NSLog statements in my
I just started out with jQuery and really only need it for some very
I just started out with C and have very little knowledge about performance issues
I just started out using wordpress as a CMS. On the site I have
I have started learning Ruby and just tried out my first hello world program
Just started learning Rails (3). I am tearing my hair out trying to find
I have started learning perl and like to try out new things. I have
i just started learning swings. And thought of trying out a simple program, but
I am just started out learning Python and also started looking into Django a
I only just started learning Python and found out that I can pass a

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.