assert(a == b, a) not DRY

8 views
Skip to first unread message

Cay Horstmann

unread,
Sep 18, 2009, 6:58:10 PM9/18/09
to scalatest-users
I started using ScalaTest today, so hopefully this is not something
obvious. In jUnit, assertEquals(a, b) tells me what the actual and
expected values are when they fail to match. I know I can use assert(a
== b, a), but it's a minor annoyance that I have to repeat a (and
capture it in a val). The spec style "a should equal(b)" is no better.
It just tells me that they should be equal but aren't. Is there an
easy way to overcome that?

Thanks,

Cay

Bill Venners

unread,
Sep 18, 2009, 7:13:30 PM9/18/09
to scalate...@googlegroups.com
Hi Cay,

You can say:

assert(a === b)

Thanks.

Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com

Bill Venners

unread,
Sep 18, 2009, 8:21:55 PM9/18/09
to scalate...@googlegroups.com
Hi Cay,

Sorry I've been at the JVM Languages Summit the past three days and
therefore slower to respond. The documentation for Assertions is here:

http://www.artima.com/scalatest/doc-1.0-SNAPSHOT/org/scalatest/Assertions.html

That's for 1.0, but it is the same for 0.9.5 (except I removed two
methods that had been deprecated for two releases already). For the
1.0 release I'll finally get a real user guide up on the web.
ScalaTest has always had very thorough documentation, but so far it
has pretty much been just in the scaladoc form.

Other than the form I mentioned in my previous email:

assert(a === b)

You can also say:

expect(a) { b }

The assert(a === b) form fails with "1 did not equal 2":

scala> import org.scalatest.Assertions._
import org.scalatest.Assertions._

scala> val a = 1
a: Int = 1

scala> val b = 2
b: Int = 2

scala> assert(a === b)
org.scalatest.TestFailedException: 1 did not equal 2
at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:278)
at org.scalatest.matchers.ShouldMatchers$.newAssertionFailedException(ShouldMatchers.scala:2318)
at org.scalatest.Assertions$class.assert(Assertions.scala:363)
at org.scalatest.matchers.ShouldMatchers$.assert(ShouldMatchers.scala:2318)
at .<init...

The === does not indicate that one value was expected and the other
actual, because it can be hard to remember which goes on which side of
the equals sign. If you want to differentiate between actual and
expected, you can use the expect form:

scala> expect(a) { b }
org.scalatest.TestFailedException: Expected 1, but got 2
at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:278)
at org.scalatest.matchers.ShouldMatchers$.newAssertionFailedException(ShouldMatchers.scala:2318)
at org.scalatest.Assertions$class.expect(Assertions.scala:577)
at org.scalatest.matchers.ShouldMatchers$.expect(ShouldMatchers.scala:2318)
at .<...
scala> expect(b) { a }
org.scalatest.TestFailedException: Expected 2, but got 1
at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:278)
at org.scalatest.matchers.ShouldMatchers$.newAssertionFailedException(ShouldMatchers.scala:2318)
at org.scalatest.Assertions$class.expect(Assertions.scala:577)
at org.scalatest.matchers.ShouldMatchers$.expect(ShouldMatchers.scala:2318)
at .<...

The value you are expecting goes in the parens after expect, and the
code that evaluates to the actual value goes in the curly braces. I
tend to use the expect form when the code that produces the actual
value is a bit longer:

expect(3) {
some.longer.expression.that.i.hope.results.in.three()
}

Can you elaborate on why the a should equal (b) didn't give you a good
output? It should print the toString of both a and b:

scala> import org.scalatest.matchers.ShouldMatchers._
import org.scalatest.matchers.ShouldMatchers._

scala> val a = 1
a: Int = 1

scala> val b = 2
b: Int = 2

scala> a should equal (b)
org.scalatest.TestFailedException: 1 did not equal 2
at org.scalatest.matchers.Matchers$class.newTestFailedException(Matchers.scala:148)
at org.scalatest.matchers.ShouldMatchers$.newTestFailedException(ShouldMatchers.scala:2318)
at org.scalatest.matchers.ShouldMatchers$ShouldMethodHelper$.shouldMatcher(ShouldMatchers.scala:871)
at org.scalatest.matchers.ShouldMatchers$IntShouldWra...

For example here is said "1 did not equal 2".

Bill

Cay Horstmann

unread,
Sep 18, 2009, 8:26:31 PM9/18/09
to scalatest-users
Very nice. Thank you!

On Sep 18, 4:13 pm, Bill Venners <b...@artima.com> wrote:
> Hi Cay,
>
> You can say:
>
> assert(a === b)
>
> Thanks.
>
> Bill
> ----
> Bill Venners
> Artima, Inc.http://www.artima.com
Reply all
Reply to author
Forward
0 new messages