Is there an easy way to stub out time.Now() globally during test?

With implementing a custom interface you are already on the right way. I take it you use the following advise from the golang-nuts thread you’ve posted: type Clock interface { Now() time.Time After(d time.Duration) <-chan time.Time } and provide a concrete implementation type realClock struct{} func (realClock) Now() time.Time { return time.Now() } func (realClock) … Read more

How to unit test a Go Gin handler function?

To test operations that involve the HTTP request, you have to actually initialize an *http.Request and set it to the Gin context. To specifically test c.BindQuery it’s enough to properly initialize the request’s URL and URL.RawQuery: func mockGin() (*gin.Context, *httptest.ResponseRecorder) { w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) // test request, must instantiate a request … Read more

What is the best way to unit-test SLF4J log messages?

Create a test rule: import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import org.slf4j.LoggerFactory; import java.util.List; import java.util.stream.Collectors; public class LoggerRule implements TestRule { private final ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); private final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); @Override public Statement apply(Statement base, Description description) { return new Statement() { @Override … Read more

Service mocked with Jest causes “The module factory of jest.mock() is not allowed to reference any out-of-scope variables” error

You need to store your mocked component in a variable with a name prefixed by “mock”. This solution is based on the Note at the end of the error message I was getting. Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable … Read more

Any suggestions for testing extjs code in a browser, preferably with selenium?

The biggest hurdle in testing ExtJS with Selenium is that ExtJS doesn’t render standard HTML elements and the Selenium IDE will naively (and rightfully) generate commands targeted at elements that just act as decor — superfluous elements that help ExtJS with the whole desktop-look-and-feel. Here are a few tips and tricks that I’ve gathered while … Read more