Testing with google closure

1,290 views
Skip to first unread message

Ngan Pham

unread,
Apr 29, 2010, 1:18:05 AM4/29/10
to closure-lib...@googlegroups.com
Hi, I have a couple questions regarding testing with google closure:

- What's the best way to test whether or a not a method on a object was called?  There seems to be no easy way of doing this... jspec has a "should receive" (like rspec) helper--is there anything equivalent for closure?

- Are there some tutorials, examples, docs other than the source code and on the API site?

Thanks in advance!

- Ngan

Shawn Brenneman

unread,
Apr 29, 2010, 2:14:45 PM4/29/10
to closure-lib...@googlegroups.com
The mock framework in the testing package can verify that specific object methods are called by your code. A mock object replaces some more complex object that your code will make calls on. You can specify what methods of the mock object should be called by your code, and the test will succeed only if those calls are made.

The documentation starts here:

As the docs say, there are some examples in the test files for StrictMock and LooseMock. It's also similar in spirit to the Java EasyMock framework, for which there are many tutorials. (And we're working on getting more documentation and tutorials published to the Closure Library site, but that's what's available now).

Shawn

Erik Arvidsson

unread,
Apr 29, 2010, 3:02:48 PM4/29/10
to closure-lib...@googlegroups.com, Péter Pallos
+pallosp who knows more about Closure testing than anyone else.

One of the problems of the Closure testing framework is that there are
too many ways to do something. In your case I can think of quite a few
ways. From least intrusive to most intrusive:

1. Override the function manually and set and increment a counter
2. goog.testing.recordFunction
3. goog.testing.createFunctionMock
4. goog.testing.createMethodMock
5. goog.testing.createLooseMock
6. goog.testing.createStrictMock

In your case I think recordFunction is the most suitable. You might
also want to use goog.testing.PropertyReplacer to ensure that the
method/function is restore afterwards.

erik

Ngan Pham

unread,
Apr 30, 2010, 1:38:05 AM4/30/10
to closure-lib...@googlegroups.com, Péter Pallos
Ah thank you! recordFunction works like a charm!

Ngan Pham

unread,
May 1, 2010, 3:23:45 PM5/1/10
to closure-lib...@googlegroups.com, Péter Pallos
Ah thank you! recordFunction works like a charm!


On Thursday, April 29, 2010, Erik Arvidsson <a...@google.com> wrote:

Alex Shaver

unread,
Feb 1, 2016, 10:41:13 AM2/1/16
to Closure Library Discuss, pal...@google.com
New to the framework, and having trouble finding good documentation on test design/structure. I'm very familiar with the googlemock/googletest framework in C++. The documentation for that project gives a pretty good guide on how to write tests in that framework, how to use mocks, etc. The link given in this post is broken, and the generated documentation is... sparse, at best, on how to actually use a thing. 

Consider, for example, the documentation on "goog.testing.testSuite": 
Runs the lifecycle methods (setUp, tearDown, etc.) and test* methods from the given object. For use in tests that are written as JavaScript modules or goog.modules.
I know in googletest, that each test in a given fixture is run independently setUp - test* - tearDown, and the variables in the fixture are initialized on each cycle. Is that the case for testSuite? it's hard to tell.

Is there a better 3rd party book or resource that gets into more detail? I've looked through the O'Reilly book on the subject, but it only covers a case or two about specific test details. Should I try to learn EasyMock in Java? How tight are the parallels between them?

Joel Martinez

unread,
Feb 1, 2016, 12:47:54 PM2/1/16
to Closure Library Discuss, Péter Pallos
Ya, the docs are lacking. The good news is, we're working on a system to export some of our internal docs to GitHub pages. Can't give an ETA right now though. 

In the meantime...

The Closure book has an entire chapter on the testing framework, and it is probably the best formal documentation on Closure-style unit testing. 

Besides that, the Closure Library source code is the next best resource. Just search for files that end in "_test.js". They will all have an associated "_test.html" file which, when opened in a browser, runs the tests from the .js file. 

Also, all of the Closure testing framework libraries live in closure/goog/testing and should be well documented.

Feel free to ask specific questions here if you get stuck.

--

---
You received this message because you are subscribed to the Google Groups "Closure Library Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-library-d...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages