Unit testing with singletons

Have a look at the Google Testing blog: Using dependency injection to avoid singletons Singletons are Pathological Liars Root Cause of Singletons Where have all the Singletons Gone? Clean Code Talks – Global State and Singletons Dependency Injection. And also: Once Is Not Enough Performant Singletons Finally, Misko Hevery wrote a guide on his blog: … Read more

To test a custom validation angularjs directive

The other answer’s tests should be written as: describe(‘directives’, function() { var $scope, form; beforeEach(module(‘exampleDirective’)); beforeEach(inject(function($compile, $rootScope) { $scope = $rootScope; var element = angular.element( ‘<form name=”form”>’ + ‘<input ng-model=”model.somenum” name=”somenum” integer />’ + ‘</form>’ ); $scope.model = { somenum: null } $compile(element)($scope); form = $scope.form; })); describe(‘integer’, function() { it(‘should pass with integer’, function() … Read more

Run JUnit tests with SBT

Finally, I’ve discovered, that I have to add the following settings to the subproject: lazy val webapp = project settings( Seq( projectDependencies ++= Seq( …. “org.scalatest” %% “scalatest” % “2.2.2” % Test, “junit” % “junit” % “4.11” % Test, crossPaths := false, “com.novocode” % “junit-interface” % “0.11” % Test ) ): _* ) It is … Read more

Testing os.Exit scenarios in Go with coverage information (coveralls.io/Goveralls)

With a slight refactoring, you may easily achieve 100% coverage. foo/bar.go: package foo import ( “fmt” “os” ) var osExit = os.Exit func Crasher() { fmt.Println(“Going down in flames!”) osExit(1) } And the testing code: foo/bar_test.go: package foo import “testing” func TestCrasher(t *testing.T) { // Save current function and restore at the end: oldOsExit := … Read more

Updating input html field from within an Angular 2 test

You’re right that you can’t just set the input, you also need to dispatch the ‘input’ event. Here is a function I wrote earlier this evening to input text: function sendInput(text: string) { inputElement.value = text; inputElement.dispatchEvent(new Event(‘input’)); fixture.detectChanges(); return fixture.whenStable(); } Here fixture is the ComponentFixture and inputElement is the relevant HTTPInputElement from the … Read more