Possible error in example code for Sharing fixtures across classes section

49 views
Skip to first unread message

Gavin Casey

unread,
Oct 4, 2013, 11:48:03 AM10/4/13
to scalate...@googlegroups.com
In the documentation there is some example code for
I was looking at this example with a view to applying it in my own tests but I am getting a compilation error with the example code 
... FixtureSharingTest.scala:35: recursive method withFixture needs result type
[error]       withFixture(test.toNoArgTest(db)) // "loan" the fixture to the test

Is there an error in the example code or is there something else I might need to take into account?



Bill Venners

unread,
Oct 4, 2013, 11:54:31 AM10/4/13
to scalate...@googlegroups.com
Hi Gavin,



That's odd that it would think it is recursive. It is actually invoking an overloaded withFixture method that takes a NoArgTest. Anyway, you can always put the result type on. Instead of:

def withFixture(test: OneArgTest) = { ...

You can write:

def withFixture(test: OneArgTest): Outcome = {
I'll double check the example, but they all compile in our build. I wonder if you don't mind posting the exact class you're compiling and what the exact error message is.

Thanks.

Bill


--
You received this message because you are subscribed to the Google
Groups "scalatest-users" group.
To post to this group, send email to scalate...@googlegroups.com
To unsubscribe from this group, send email to
scalatest-use...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/scalatest-users?hl=en
ScalaTest itself, and documentation, is available here:
http://www.artima.com/scalatest
---
You received this message because you are subscribed to the Google Groups "scalatest-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalatest-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



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

Gavin Casey

unread,
Oct 6, 2013, 12:03:39 PM10/6/13
to scalate...@googlegroups.com
Hi Bill,
Isolating the code to a simple source file :

package com.xyz.ftest

import java.util.concurrent.ConcurrentHashMap
import org.scalatest._ //fixture

import DbServer._
import java.util.UUID.randomUUID

object DbServer { // Simulating a database server
  type Db = StringBuffer
    private val databases = new ConcurrentHashMap[String, Db]
      def createDb(name: String): Db = {
            val db = new StringBuffer
                databases.put(name, db)
                    db
                      }
        def removeDb(name: String) {
              databases.remove(name)
                }
}
trait DbFixture { this: fixture.Suite =>

  type FixtureParam = Db

// Allow clients to populate the database after
//   // it is created
   def populateDb(db: Db) {}

   def withFixture(test: OneArgTest): Outcome = {
     val dbName = randomUUID.toString
     val db = createDb(dbName) // create the fixture
     try {
       populateDb(db) // setup the fixture
       withFixture(test.toNoArgTest(db)) // "loan" the fixture to the test
     }
     finally removeDb(dbName) // clean up the fixture
     }
   }

class ExampleSpec extends fixture.WordSpec with DbFixture {

    override def populateDb(db: Db) { // setup the fixture
      db.append("ScalaTest is ")
    }

    "Testing" should {
      "should be easy" in { db =>
         db.append("easy!")
         assert(db.toString === "ScalaTest is easy!")
      }

      "should be fun" in { db =>
        db.append("fun!")
        assert(db.toString === "ScalaTest is fun!")
      }
   }

  // This test doesn't need a Db
  "Test code" should {
    "should be clear" in { () =>
    val buf = new StringBuffer
    buf.append("ScalaTest code is ")
    buf.append("clear!")
    assert(buf.toString === "ScalaTest code is clear!")
  }
 }
}

and compiling with scala 2.10.2
%scalac -cp ~/.ivy2/cache/org.scalatest/scalatest_2.10/jars/scalatest_2.10-2.0.M8.jar sharedfixture.scala 
Gives 
sharedfixture.scala:33: error: recursive method withFixture needs result type
       withFixture(test.toNoArgTest(db)) // "loan" the fixture to the test

Putting in the : Outcome return type fixes the above.

Regards,

Gavin

Komsit Prakobphol

unread,
Jul 4, 2016, 11:17:57 PM7/4/16
to scalatest-users
I've found the same issue in version 2.2.6 in this scaladoc http://doc.scalatest.org/2.2.6/#org.scalatest.fixture.FunSpec. Adding return type ": Outcome" fixed the issue

package org.scalatest.examples.funspec.oneargtest

import org.scalatest.fixture import java.io._
class ExampleSpec extends fixture.FunSpec {
case class FixtureParam(file: File, writer: FileWriter)
def withFixture(test: OneArgTest) = {
// create the fixture val file = File.createTempFile("hello", "world") val writer = new FileWriter(file) val theFixture = FixtureParam(file, writer)
try { writer.write("ScalaTest is ") // set up the fixture withFixture(test.toNoArgTest(theFixture)) // "loan" the fixture to the test } finally writer.close() // clean up the fixture }
describe("Testing") { it("should be easy") { f => f.writer.write("easy!") f.writer.flush() assert(f.file.length === 18) }
it("should be fun") { f => f.writer.write("fun!") f.writer.flush() assert(f.file.length === 17) } } }
Reply all
Reply to author
Forward
0 new messages