setup and teardown for each test using std testing package

Update for Go 1.14 (Q1 2020) The testing package now supports cleanup functions, called after a test or benchmark has finished, by calling T.Cleanup or B.Cleanup respectively. Example, func TestFunction(t *testing.T) { // setup code // sub-tests t.Run() t.Run() … // cleanup t.Cleanup(func(){ //tear-down code }) } Here, t.Cleanup runs after the test and all … Read more

Error with XCTestExpectation: API violation – multiple calls made to -[XCTestExpectation fulfill]

XCTestExpectation Error API violation – multiple calls made to -[XCTestExpectation fulfill] I got the same error when set expectation.expectedFulfillmentCount. Solution: expectation.assertForOverFulfill = false If expectation.assertForOverFulfill = true and fulfill() is called when it is already fulfilled then it throws an exception

Type or namespace name ‘VisualStudio’ does not exist in the namespace ‘Microsoft’

I got this error when trying to build a project in TFS. These steps fixed it: remove reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework add nuget MSTest.TestFramework add nuget MSTest.TestAdapter (optional but needed to run tests inside Visual Studio) The above steps caused this element to be removed from my .csproj file: <Reference Include=”Microsoft.VisualStudio.QualityTools.UnitTestFramework” /> And these two were … Read more

Mocking extension function in Kotlin

I think MockK can help you. It supports mocking extension functions too. You can use it to mock object-wide extensions: data class Obj(val value: Int) class Ext { fun Obj.extensionFunc() = value + 5 } with(mockk<Ext>()) { every { Obj(5).extensionFunc() } returns 11 assertEquals(11, Obj(5).extensionFunc()) verify { Obj(5).extensionFunc() } } If you extension is a … Read more

How to unit test angularjs controller with $location service

This is how my test ended up working. describe(‘OpponentsCtrl’, function() { var scope, rootScope, ctrl, location; beforeEach(inject(function($location, $rootScope, $controller) { location = $location; rootScope = $rootScope; scope = $rootScope.$new(); ctrl = $controller(OpponentsCtrl, {$scope: scope}); })); it(‘should change location when setting it via show function’, function() { location.path(‘/new/path’); rootScope.$apply(); expect(location.path()).toBe(‘/new/path’); // test whatever the service should … Read more