混合Assert和Act步骤可以吗?AAA更像是一条指导方针吗?或者我错过了什么?
这是我的测试:
[TestMethod]
public void CancelButtonSelected_DontCancelTwiceThenCancel_DialogCloses()
{
// Arrange
IAddAddressForm form = Substitute.For<IAddAddressForm>();
// Indicate that when Show CancelMessage is called it
// should return cancel twice (saying we want to cancel the cancel)
// then it should return ok
form.ShowCancelMessage().Returns(DialogResult.Cancel,
DialogResult.Cancel, DialogResult.OK);
AddAddressController controller = new AddAddressController(form);
AddressItem item = TestHelper.CreateAddressBob();
// Act
EnterAddressInfo(form, controller, item);
controller.CancelButtonSelected();
Assert.IsTrue(form.DialogResult == DialogResult.None);
controller.CancelButtonSelected();
Assert.IsTrue(form.DialogResult == DialogResult.None);
controller.CancelButtonSelected();
// Assert
Assert.IsTrue(form.DialogResult == DialogResult.Cancel);
}
Run Code Online (Sandbox Code Playgroud)
所以我称之为方法3次.每次通话后,我都想确保我们没有真正取消对话.然后在第三次调用时,应该取消该对话框.
这是"合法"使用AAA语法/样式吗?
我有一个方法,调用模拟对象的另一个方法,其参数是根据我传递给测试函数的参数计算的.
如何验证我测试的方法正在调用的方法是否正确调用.
我正在使用Moq.
编辑:
因为没有人得到我的意思(或者我不明白你们为我解决了这个问题的事实)我会更加具体.
我有以下方法签名:
IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state);
Run Code Online (Sandbox Code Playgroud)
在里面它应该实例化一个SocketAsyncEventArgs对象并使用正确的SocketAsyncEventArgs调用它的SetBuffer方法.
SocketAsyncEventArgs是一个实现细节,但它是实现我之后的功能的唯一方法所以我必须检查它是否正确调用了SetBuffer方法.如何确保BeginWrite()实现确实使用正确的参数调用SetBuffer?
编辑2:
这里有一些代码来澄清我的意思:
public IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
{
// No mocking can be done here
SocketAsyncEventArgs args = new SocketAsyncEventArgs
{
// Proper initialization. Should be verified as well.
};
args.SetBuffer(buffer, offset, size);
Client.SendAsync(args);
}
Run Code Online (Sandbox Code Playgroud)