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

  • Home
  • SEARCH
  • 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 8947429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:45:32+00:00 2026-06-15T12:45:32+00:00

As I understand, in unit testing methods should be isolated from their dependencies ,

  • 0

As I understand, in unit testing methods should be isolated from their dependencies, so that they wouldn’t be affected by environment changes.

Nevertheless, stubbing out all the dependencies makes me feel like I am testing implementation instead of behavior.

In other words, by isolating dependencies I am coupling my tests to the implementation details. Therefore, any code refactoring would cause failures of tests, even though behavior (the desired outcome) didn’t change.

For instance, this is a simple (Ruby) method:

  def send_request
    update_attributes(response.page_params) if active?
  end

And these are my two isolated tests for this single line of code:

  let(:page) { Page.new }

  describe '#send_request' do
    context 'when a page is active' do
      it 'updates page with the response parameters' do
        page.active = true
        response = double('response')
        page_params = double('page_params')
        response.stub(:page_params).and_return(page_params)
        page.stub(:response).and_return(response)
        page.stub(:update_attributes).and_return(nil)
        page.should_receive(:update_attributes).with(page_params)
        page.send_request
      end
    end
    context 'when a page is inactive' do
      it 'does NOT send a request' do
        page.active = false
        page.should_not_receive(:response)
        page.send_request
      end
    end
  end

The tests are passing, but I see a few serious problems:

  • If later I decide to use any other method than update_attributes() to persist changes into database, my tests will fail, even though the data will be saved as expected
  • If the implementation of response.page_params changes, my software will fail in production, but the tests will still be passing

I must be doing something wrong.

What is the right way of writing unit tests?

  • 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-15T12:45:33+00:00Added an answer on June 15, 2026 at 12:45 pm

    I don’t think you are completely off mark here, as AlistairIsrael said.

    There are a few optimisations you can do to make it more succinct. A good test should clearly show what you are expecting from your code.

    let(:page) { Page.new }
    
    describe '#send_request' do
      context 'when a page is active' do
        it 'updates page with the response parameters' do
          page.active = true
          response = double('response',
            :page_params => page_params = mock('page_params')
          )
    
          # not needed as .should_receive creates a nil stub by default.
          # page.stub(:update_attributes).and_return(nil)
          page.should_receive(:update_attributes).with(page_params)
          page.send_request
        end
      end
      context 'when a page is inactive' do
        it 'does NOT send a request' do
          page.active = false
    
          subject.should_not_receive(:update_attributes)
          page.send_request
        end
      end
    end
    

    From a few changes above you can see that rspec’s double helper is very very powerful, you can construct complex objects and using some assignment stuff you can have access to the last evaluated method in a chain.

    I made an assumption for the negative case, but you should get the idea. Testing for the method call of update_attributes is probably easier and is clearer as you know that page_params will never get called if the active? condition isn’t met.

    HTH

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

Sidebar

Related Questions

I understand that in my unit test I should do BOOST_TEST_MESSAGE(message); instead of std::cout
I'm getting started unit testing my PHP application with PHPUnit. I understand that it's
I understand that the em measurement is a relative unit for font-size, relative to
I understand that the Express edition doesn't template unit test projects... but being a
I understand, from MSDN, that ClassInitialize is to mark a method that will do
I understand that the InternalVisibleTo attribute is used to expose types and methods with
I want to understand exactly what unit test means. From what I have understood
I am unit-testing private methods in C# using NUnit. For example, my method (if
I'm trying to understand how unit testing works. So far i understand you're testing
I understand the concept of Unit Testing as coming up with simple ideas about

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.