我发现的Mock.Expect示例中,除了0或1(从不,一次)以外,没有任何一个使用Times。我修改了Foq.Usage.fsx中的一个现有测试,以尝试一些不是0或1的测试,但我无法使它工作。
有人看到什么问题了吗?
let [<Test>] ``expect method is called the specified number of times`` () =
// Arrange
let xs = Mock.Of<System.Collections.Generic.IList<int>>()
// Assert (setup)
Mock.Expect(<@ xs.Contains(0) @>, never)
Mock.Expect(<@ xs.Contains(1) @>, once)
Mock.Expect(<@ xs.Contains(2) @>, exactly 2)
// Act
xs.Contains(1) |> ignore
xs.Contains(2) |> ignore
xs.Contains(2) |> ignore
// Assert
verifyAll xs发布于 2014-01-29 00:11:40
正如评论中所讨论的,这并不能回答你的问题,但是.我会写这个(有用的):
let xs = Mock.Of<System.Collections.Generic.IList<_>>()
// Act
xs.Contains(1) |> ignore
xs.Contains(2) |> ignore
xs.Contains(2) |> ignore
// Assert
verify <@ xs.Contains(0) @> never
verify <@ xs.Contains(1) @> once
verify <@ xs.Contains(2) @> <| exactly 2发布于 2014-01-29 03:43:31
这是福克中的一个bug,当您使用Mock.Expect时,它会检查在模拟调用时是否满足期望。如果未满足给定期望,则抛出异常。这在理论上可能是有用的,因为它在未满足期望的精确点上给出了堆栈跟踪。
exactly 2的问题是在一次调用之后首先检查期望,而不是完全是两次。实际上,在调用时,我们希望最多将exactly 2解释为两个,而在verifyAll则解释为两个。
您可以使用Mock.Expect和/或在效果之后使用atmost 2和/或普通的旧验证来接近我认为现有的实现所需要的行为:
let [<Test>] ``expect method is called the specified number of times`` () =
// Arrange
let xs = Mock.Of<System.Collections.Generic.IList<int>>()
// Assert (setup)
Mock.Expect(<@ xs.Contains(0) @>, never)
Mock.Expect(<@ xs.Contains(1) @>, once)
Mock.Expect(<@ xs.Contains(2) @>, atmost 2)
// Act
xs.Contains(1) |> ignore
xs.Contains(2) |> ignore
xs.Contains(2) |> ignore
// Assert
Mock.Verify(<@ xs.Contains(2) @>, exactly 2)
verifyAll xs谢谢你报道这个问题。这个问题现在已在源代码中得到解决,并将在下一个版本中提供。
发布于 2014-01-29 00:22:54
谢谢你鲁本。我同意,显式的verify是最好的,但是(也是根据您的建议)下面的代码是可行的,以防有人想要使用Expect/verifyAll。
let [<Test>] ``expect method is called the specified number of times`` () =
// Arrange
let xs = Mock.Of<System.Collections.Generic.IList<int>>()
// Act
xs.Contains(1) |> ignore
xs.Contains(2) |> ignore
xs.Contains(2) |> ignore
// Assert
Mock.Expect(<@ xs.Contains(0) @>, never)
Mock.Expect(<@ xs.Contains(1) @>, once)
Mock.Expect(<@ xs.Contains(2) @>, exactly 2)
verifyAll xshttps://stackoverflow.com/questions/21418522
复制相似问题