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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:55:33+00:00 2026-06-08T23:55:33+00:00

This example is contrived, please don’t take it verbatim as my code. I have

  • 0

This example is contrived, please don’t take it verbatim as my code.

I have the need to assert something like the following:

def mymethod
    Dir.chdir('/tmp') do
        `ls`
    end
end

In the end I want to assert that:

  1. Dir.chdir is invoked with the appropriate parameters.
  2. ` is invoked with the appropriate parameters

I started off with…

Dir.expects(:chdir).with('/tmp')

but after that I’m not sure how to invoke the block passed to Dir.chdir.

  • 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-08T23:55:36+00:00Added an answer on June 8, 2026 at 11:55 pm

    You need to use the mocha yields method. Also, writing an expectation for the backtick method is rather interesting. You need to make an expectation like this:

    expects("`")
    

    But on what object? You might think on Kernel or Object, but that doesn’t actually work.

    As an example, given this module:

    module MyMethod
      def self.mymethod
        Dir.chdir('/tmp') do
          `ls`
        end
      end
    end
    

    I could write a test like this:

    class MyMethodTest < Test::Unit::TestCase
      def test_my_method
        mock_block = mock
        mock_directory_contents = mock
        MyMethod.expects("`").with('ls').returns(mock_directory_contents)
        Dir.expects(:chdir).yields(mock_block).returns(mock_directory_contents)
        assert_equal mock_directory_contents, MyMethod.mymethod
      end
    end
    

    Part of the trick is to figure out which object to expect the backtick method to be invoked on. It depends on the context – whatever self is when that method is invoked. Here it is the module MyMethod, but depending on where you define mymethod it will be different.

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

Sidebar

Related Questions

I know this is a contrived example so please do not jump all over
This example is highly contrived so bear with me. I have two Java classes,
This is a contrived example, but it illustrates my problem much more concisely then
Consider this contrived, trivial example: var foo = new byte[] {246, 127}; var bar
This example shows what I am trying to do: http://jsfiddle.net/Dqjvy/ I would like it
in this example http://sorgalla.com/projects/jcarousel/examples/static_controls.html i need an active state on li so it should
Please look at this peice of code :- #include<stdio.h> int main() { int arr[2][2]={1,2,3,4};
This is probably best explained through some code. I know that in the following
Suppose I have this code: -(SomeOtherType*) getMyObject { SomeType someObject = [[SomeType alloc] init];
I have an object that looks like this. List<List<string>> MyData; The inner List<string> can

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.