Chai.js not specifying which assertion is failing in a test - unit-testing

I'm using mocha with chai.js for CoffeeScript unit testing. I have a grunt task to compile the coffee files to the test folder and start PhantomJS to run the mocha tests.
Everything works fine however chai.js only indicates what test is failing and what are the expected and actual values, it does not specify what assertion is not passing in a test case. Is there any good way to print the assertion or at least the index of the assertion that is failing? I also turned on chai.Assertion.includeStack however that only shows the line number in the JavaScript file that is not that helpful for the CoffeeScript tests.
FizzBuzzTest.coffee
fizz = new FizzBuzz()
describe "Print numbers from 1 to 100", ->
it "First 10 digits from 1 to 5", ->
result = fizz.do()
arr = result.split(fizz.Delimiter)
expect(arr[0]).to.equal("1")
expect(arr[1]).to.equal("2")
expect(arr[2]).to.equal(fizz.Fizz)
expect(arr[3]).to.equal("3") # this assertion should fail
expect(arr[4]).to.equal(fizz.Buzz)
Executing tests
$ grunt test
Running "mocha:run" (mocha) task
Testing: Content/runner.html
Print numbers from 1 to 100
✓ First 10 digits from 1 to 5
1) Last 10 digits from 95 to 100
✓ Print FizzBuzz instead of number which is divisible by both 3 and 5
✓ Check number
✓ Check Fizz
✓ Check Buzz
✓ Check FizzBuzz
✓ Check if > 100 then null
✓ Check if < 1 then null
8 passing (113ms)
1 failing
1) Print numbers from 1 to 100 Last 10 digits from 95 to 100:
AssertionError: expected true to be false
at file:///Users/milan/Sites/CoffeeTests/Content/js-libs/chai/chai.js:918
at file:///Users/milan/Sites/CoffeeTests/Content/js-libs/chai/chai.js:1159
at file:///Users/milan/Sites/CoffeeTests/Content/js-libs/chai/chai.js:3563
>> 1/9 tests failed (0.11s)
Warning: Task "mocha:run" failed. Use --force to continue.
Aborted due to warnings.
Question:
Is there a good way to set chai.js to be more specific about where the AssertionError happened?
Like: AssertionError: expected true to be false in line expect(arr[3]).to.equal("3")
Thank you.

If no one comes up with a more sophisticated solution to make chai.Assertion.includeStack work nicely with CoffeeScript code, you can always pass messages to some of the functions in the expect interface:
it("bluh", function () {
chai.expect(false).to.be.equal(true, "here");
});
The "here" string is a message that will be output with the error message when the assertion fails. Take a look at the documentation though because not all functions take an optional message. I first wanted to use to.be.true above but the true method does not take an optional message.
The assert interface supports optional messages on more functions than the expect interface does.

Related

Cannot run remote function inside Unit test

im trying to setup some unit tests with Elixir but running into this error below. What am I doing wrong?
cannot invoke remote function PropertyManager.Database.get/0 inside match
Here is my code:
property_manager_test.exs
defmodule PropertyManagerTest do
use ExUnit.Case
test "the truth" do
assert 1 + 1 == 2
end
test "get value from db" do
assert PropertyManager.Database.get() = "test this"
end
end
database.ex
defmodule PropertyManager.Database do
def get do
"test this"
end
end
Try with == instead of =
What you're doing in your code is a pattern match, which means it will try to match the right side to the pattern on the left side. A pattern can't contain function calls, which is probably the cause of your error.

'2 of 3 tests passed' when only 2 test methods were exported

This is test-main.js, basically the default after running cfx init ..
var main = require("./main");
exports["test main"] = function(assert) {
assert.pass("Unit test running!");
};
exports["test main async"] = function(assert, done) {
assert.pass("async Unit test running!");
done();
};
require("sdk/test").run(exports);
And this is the output, why does it say 2 of 3 tests passed?
When there is only 2 test methods exported.
[rob#laptop addon]$ ~/apps/addon-sdk-1.17/bin/cfx test
Using binary at '/usr/bin/firefox'.
Using profile at '/tmp/tmpdrQaVV.mozrunner'.
Running tests on Firefox 31.1.0/Gecko 31.1.0 ({ec8030f7-c20a-464f-9b0e-13a3a9e97384}) under linux/x86_64.
console.warn: addon: Plural form unknown for locale "null"
System JS : ERROR chrome://browser/content/browser.js:14338 - TypeError: value is not a non-null object
System JS : ERROR chrome://browser/content/content-sessionStore.js:230 - TypeError: docShell.QueryInterface(...).sessionHistory is null
console.error: addon:
warnings and/or errors were logged.
2 of 3 tests passed.
Total time: 3.634853 seconds
Program terminated unsuccessfully.

Spock interaction based test does not fail

I have the following two Spock tests:
def "sends a valid response when no users exist"() {
setup:
def exchange = new HttpServerExchange(Mock(ServerConnection))
usersRepository.size() >> 0
when:
firstRunHandler.handleRequest(exchange)
then:
1*response.send(exchange, _)
}
def "does not send content when any users exist"() {
setup:
usersRepository.size() >> 1
when:
firstRunHandler.handleRequest(new HttpServerExchange(Mock(ServerConnection)))
then:
0*response.send(_, _)
}
The second one should definitely fail, since the interaction is stll there. But it always passes. I can not even make it fail with:
then:
assert false
0*response.send(_, _)
IntelliJ Idea still shows it as "green". But when I change the "then" to
then:
assert false
the test fails, so it is definitely being run and executed as a spock test.
I don't get much info from Spock, and I did not find out anything when debugging. What am I missing? What can I do to diagnose this problem?

More informative asserts in Scala

I'm looking for asserts in the style of Google's testing framework, where something like ASSERT_LT(a, b) will check that $a is less than $b, and if not, will print the values of $a and $b in the error message. The asserts I've found so far just stop execution without printing anything useful by default. Any pointers?
Bonus: I like to write assertion-heavy code (not just in the tests), so it would be nice if the assertions are quick to evaluate.
Context: I came across this when writing unittests using this code as a template.
Specs2 defines a number of matchers such as must be_<=. At first I thought these needed to integrated into a specification, but at the bottom of the matchers page it says that they are modular functionality that "you should be able to reuse in your own test
framework". As an example, "You can reuse [traits such as] org.specs2.matcher.MustMatchers to write anything like 1 must be_==(1) and
get a Result back".
ScalaTest has its own matchers, as well. For example, one must be < (7).
def assert_<[A <% Ordered[A]](a: => A, b: => A) {
assert(a < b, "Assertion failed: expected %s < %s" format (a, b))
}
scala> assert_<(2, 1)
java.lang.AssertionError: assertion failed: Assertion failed: expected 2 < 1

How to build a test group in watir?

I have some single watir.rb scripts that use IE and are written in a standard watir way.
How do I create a test group that combines them? I want all of them be executed by executing the main script.
Is it possible to auto include single test files into a test group by subidr?
Is it possible to enumerate the files that should be included in the test group?
Can I cascade (include other watir test groups in a watir test group)?
Edit: After much searching and googling I could not find anything.
I will use this simple style for now:
passed = 0
failed = 0
result = system("ruby suite_one.rb") #execute the script and wait for it to finish.
if result #Record our results.
passed = passed + 1
else
failed = failed + 1
end
result = system("ruby suite_two.rb") #execute the script and wait for it to finish.
if result #Record our results.
passed = passed + 1
else
failed = failed + 1
end
puts "failed: "
puts failed
puts "passed: "
puts passed
Take a look how Watir unit tests are executed.