gpt4 book ai didi

c# - 在单元测试的开关案例中的特定情况下,如何仅调用一个函数?

转载 作者:太空宇宙 更新时间:2023-11-03 12:38:46 25 4
gpt4 key购买 nike

所以我有一个可以有 4 个状态的 Doorcontrol 类

public enum DoorControlState
{
DoorClosed,
DoorOpening,
DoorOpen,
DoorBreached

}

要打开门,需要访问代码,然后验证代码,如果正确则打开门。如果门不处于关闭状态,则不会发生任何事情。它应该像这样 RequestEntry(id)[Doorcontrol]->ValidateEntryRequest(id)[IUserValidation]->OK[Doorcontrol]->Open[IDoor]->将状态更改为 DoorOpening。然后 Idoor 调用 DoorOpen,状态变为 DoorOpen 等等。

我已经按如下方式实现了 DoorControl 类

public class DoorControl
{
private IDoor _door;
private IEntryNotification _entryNotification;
private IUserValidation _userValidation;
private DoorControlState _doorControlState;
private int _id;

public DoorControl(IDoor door, IEntryNotification entryNotification, IUserValidation userValidation, DoorControlState doorControlState)
{
_door = door;
_entryNotification = entryNotification;
_userValidation = userValidation;
_doorControlState = doorControlState;
}

public void Run()
{
switch (_doorControlState)
{
case DoorControlState.DoorClosed:

bool foo = RequestEntryId();
if (foo)
{
_door.Open();
_doorControlState = DoorControlState.DoorOpening;
}
break;
case DoorControlState.DoorOpening:
_door.Close();
_doorControlState = DoorControlState.DoorOpen;
break;
case DoorControlState.DoorOpen:
// Do stuff....
break;
case DoorControlState.DoorBreached:
// Do stuff
break;
default:
throw new InvalidEnumArgumentException();
}
}

private bool RequestEntryId()
{
bool maybeFoo = _userValidation.ValidateEnetryRequest();
if (maybeFoo = true)
{
return true;
}

return false;
}

public void DoorOpened()
{
//
}

public void DoorClosed()
{
//
}
}

UserValidation 和 Door 类作为接口(interface)实现

public interface IUserValidation
{
bool ValidateEnetryRequest();

}

public interface IDoor
{

void Open();

void Close();

}

我正在使用 Nunit 和 NSubstitute 测试我的代码

namespace DoorControl.Unit.Test
{
[TestFixture]
public class DoorControlUnitTest
{
private DoorControl _uut;
private IDoor _door;
private IEntryNotification _entryNotification;
private IUserValidation _userValidation;
private DoorControlState _doorControlState;

[SetUp]

public void SetUp()
{
_door = Substitute.For<IDoor>();
_entryNotification = Substitute.For<IEntryNotification>();
_userValidation = Substitute.For<IUserValidation>();
_doorControlState = DoorControlState.DoorClosed;

_uut = new DoorControl(_door, _entryNotification, _userValidation, _doorControlState);
}

[Test]
public void InputCorrectId()
{
_userValidation.ValidateEnetryRequest().Returns(true);
_uut.RequestEntryId(); // Can't called that cause it's private

_door.Received().Open();

}

}
}

我无法弄清楚如何让它仅在处于 Doorclosed 状态时调用 RequestEntryId,而在处于其他状态时忽略它。

编辑:Doorcontrol 所处的状态应该对其他人隐藏。

最佳答案

public void InputCorrectId()
{
_userValidation.ValidateEnetryRequest().Returns(true);
if(_doorControlState == DoorControlState.DoorClosed){
_uut.RequestEntryId(); // Make it public
}
_door.Received().Open();

}

或者像这样进入门控类:

 case DoorControlState.DoorClosed:
RequestEntryId();

关于c# - 在单元测试的开关案例中的特定情况下,如何仅调用一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39842721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com