scalamock / specs2:如果我没有断言怎么办?期望仅在IsolatedMockFactory中

时间:2015-05-11 08:30:39

标签: specs2 scalamock

如果我在Specs2测试中实际上没有像count must_== 1 a那样的明确断言,我会收到一个错误,表明找不到隐式。

// doesn't compile
class Example extends Specification {
  "You need an assertion" >> {
    // hello!
  }
}

足够公平。

但如果我也使用scalamock的MockContext,我可以依靠期望而不是断言; mock mock和scalamock将验证方法被调用等;

class MockExample extends Specification {
  "I can use 'expectations' here instead" in new MockContext {
    val foo = mock[Foo]
    (foo.bar _).expects(*).once
    // no explicit assertions
  }     
}

但是,如果我尝试通过混合IsolatedMockFactory来共享上下文设置,那么我将回到编译器失败状态。任何想法如何解决它?

// doesn't compile
class AnotherMockExample extends Specification with IsolatedMockFactory {
  val foo = mock[Foo]
  "I can't use 'expectations' here any more" >> {        
    (foo.bar _).expects(*).once
  }  
}

1 个答案:

答案 0 :(得分:1)

An example in specs2 accepts anything that has an if (control.HasControls()) { var result = GetTextBoxByID(control.Controls, id); if (result != null) return result; } typeclass instance. Since org.specs2.execute.AsResult is of type (foo.bar _).expects.once you can create an CallHandler instance for AsResult that just evaluates the value and returns CallHandler

Success

Since failures are exception-based in ScalaMock this should result in an exception being thrown if some mock expectation is not satisfied.