Tests/Get-AppFromLastSuccessfulBuild.Tests.ps1

Describe Get-AppFromLastSuccessfulBuild {
    InModuleScope Tecman.Tfs.Tools {
        Mock Get-ProjectName {'Test Project'}
        Mock Get-TFSCollectionURL {'https://dev.azure.com/test'}
        Mock Invoke-TFSAPI {return $null}
        Mock Invoke-TFSAPI {return $null} -ParameterFilter {$Url -eq 'https://dev.azure.com/testTest Project/_apis/build/builds?queryOrder=finishTimeDescending&resultFilter=succeeded&$top=1&branchName=refs/heads/Test Branch'}
        
        It 'Should not specify branch in API URL when branch name not passed' {
            Get-AppFromLastSuccessfulBuild -ProjectName 'Test Project'
            Assert-MockCalled Invoke-TFSAPI -ParameterFilter {$Url -eq 'https://dev.azure.com/testTest Project/_apis/build/builds?queryOrder=finishTimeDescending&resultFilter=succeeded&$top=1&branchName=refs/heads/Test Branch'} -Times 0
        }

        It 'Should specify branch in API URL when branch name is passed' {
            Get-AppFromLastSuccessfulBuild -ProjectName 'Test Project' -BranchName 'Test Branch' | should be $null
            Assert-MockCalled Invoke-TFSAPI -ParameterFilter {$Url -eq 'https://dev.azure.com/testTest Project/_apis/build/builds?queryOrder=finishTimeDescending&resultFilter=succeeded&$top=1&branchName=refs/heads/Test Branch'} -Times 1
        }
    }
}