Code Monkey home page Code Monkey logo

Comments (3)

kamok avatar kamok commented on May 10, 2024 1

Here is my current "hack: around existing feature:

  Describe 'checkout_release_branch()'
    Mock git
      echo 'Mock Test: git' "$@"
    End
    Describe 'when branch does not exist'
      It 'it calls git checkout with proper args'
        When call 'checkout_release_branch' 'my-release-name'

        The status should be success
        The stdout should include 'Mock Test: git checkout --orphan my-release-name --quiet'
      End
    End
  End

Not great, but better than nothing.

from shellspec.

kamok avatar kamok commented on May 10, 2024

This is very much needed and I'm surprised nobody else has posted about this.

I don't believe there are any matchers now equivalent to RSpec's "Expect mock to have been called with" matcher.

A use-case for shell scripting would be calling git with certain arguments.
Eg, "Expect code to call git pull --ff-only" when X condition happens, and git pull --rebase true when Y condition happens

We're not testing the git command, we're testing that git was called with certain args. Very important to test.

from shellspec.

ksaito1125 avatar ksaito1125 commented on May 10, 2024

I find it useful to be able to record and test calls like MagicMock in pytest.

I prepared the following code.

$ cat ./spec/magic_mock.sh 
#! /bin/sh

OUTPUT=${0##./spec/}.called
echo $* >> ${OUTPUT}

Mock the git command.

Describe 'hellogit'
    Include lib/hello.sh

    Mock git
        ./spec/magic_mock.sh git $*
        echo $*
    End

    It 'git show'
        When call hellogit https://github.com/shellspec/shellspec.git
        The stdout should eq "show v0.1.0"
    End
End

When executed, the call will be recorded in the magic_mock.called file.

$ shellspec
Running: /bin/sh [sh]
.

Finished in 0.09 seconds (user 0.08 seconds, sys 0.02 seconds)
1 example, 0 failures

$ cat magic_mock.sh.called 
git clone https://github.com/shellspec/shellspec.git
git show v0.1.0
$ 

I don't know how to test this result.
For example, I would like to test the results as follows.

    It 'git show'
        When call hellogit
        The callwith[0] eq "git clone https://github.com/shellspec/shellspec.git"
        The callwith[1] eq "git show v0.1.0"
    End

The code I tested is below.

hellogit() {
    git clone $1 > /dev/null 2>&1
    git show v0.1.0
}

from shellspec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.