2013-01-11 41 views
5

我有一个非常简单的测试,我试图嘲笑一个特质。该测试甚至没有运行,并且因初始化错误而失败:java.lang.IllegalArgumentException:需求失败:是否记得使用withExpectations?如何使用ScalaMock代理模拟?

这是我非常简单的测试:

import org.scalatest._ 
import org.junit.runner.RunWith 
import org.scalatest.junit.JUnitRunner 
import org.scalatest.matchers.ShouldMatchers 
import org.scalamock.ProxyMockFactory 
import org.scalamock.scalatest.MockFactory 

@RunWith(classOf[JUnitRunner]) 
class TurtleSpec extends FunSpec with MockFactory with ProxyMockFactory { 
    trait Turtle { 
    def turn(angle: Double) 
    } 

    val m = mock[Turtle] 
    m expects 'turn withArgs (10.0) 

    describe("A turtle-tester") { 
    it("should test the turtle") { 
     m.turn(10.0) 
    } 
    } 
} 

回答

1

需要调用resetMocks/resetExpectations运行测试,这样做是最好的方式(ScalaTest方式)前:

class TurtleSpec extends FunSpec with MockFactory with ProxyMockFactory with BeforeAndAfter { 

    before { 
    resetMocks() 
    resetExpectations() 
    } 

    ... 
} 
相关问题