英文:
Chain assertions together in kotlin
问题
我有一个操作,我希望它在两种不同的条件下都成功。例如,如果状态代码是501 OR 消息不是 'FAILED'。是否有一种方式可以将断言逻辑地分组在一起,就像AND/OR一样。如果断言1通过或者断言2通过,我希望我的测试用例成功。
英文:
I have an operation that I want to succeed with two different conditions. For example, if the status code is 501 OR message is not 'FAILED'. Is there a way to have assertions grouped together logically like AND/OR. If assertion 1 passes OR assertion 2 passes, I want my test case to succeed.
答案1
得分: 1
@cactustictacs建议将您的断言进行命名,然后将它们链接在一起。
我将通过展示验证具有一些复杂排列的4个REST接口输入的代码来提出建议,这些排列允许/不允许一些情况。这段代码可能比经典的if ... else
结构更容易阅读。
看看它们如何使用when
构造进行评估。也许您可以借鉴这些想法来创建您自己的断言...
val userIdNotNull = userId != null
&& channelType == null
&& primaryMentorId == null
&& primaryClinicianId == null
val userIdAndChannelTypeNotNull = userId != null
&& channelType != null
&& primaryMentorId == null
&& primaryClinicianId == null
val primaryMentorIdNotNull = userId == null
&& channelType == null
&& primaryMentorId != null
&& primaryClinicianId == null
val primaryClinicianIdNotNull = userId == null
&& channelType == null
&& primaryMentorId == null
&& primaryClinicianId != null
val channels = when {
userIdNotNull -> getChannelsByUserId(userId)
userIdAndChannelTypeNotNull -> channelRepository.findByMemberIdAndChannelType(userId!!, channelType!!)
.ifEmpty { throw NotFoundException() }
primaryMentorIdNotNull -> channelRepository.findByPrimaryMentorId(primaryMentorId)
.ifEmpty { throw NotFoundException() }
primaryClinicianIdNotNull -> channelRepository.findByPrimaryClinicianId(primaryClinicianId)
.ifEmpty { throw NotFoundException() }
else -> throw InvalidRequestParameterException("This combination of request parameters is not supported")
}
希望这有所帮助。
英文:
@cactustictacs suggests "naming" your assertions and then chaining them.
I'll suggest an answer by showing code that validates 4x REST interface inputs that have some complex permutations that allowed / not allowed. This code is arguably easier to read than a classic if ... else
structure.
See how they are evaluated using the when
construct. Perhaps you can take these ideas for your assertions...
val userIdNotNull = userId != null
&& channelType == null
&& primaryMentorId == null
&& primaryClinicianId == null
val userIdAndChannelTypeNotNull = userId != null
&& channelType != null
&& primaryMentorId == null
&& primaryClinicianId == null
val primaryMentorIdNotNull = userId == null
&& channelType == null
&& primaryMentorId != null
&& primaryClinicianId == null
val primaryClinicianIdNotNull = userId == null
&& channelType == null
&& primaryMentorId == null
&& primaryClinicianId != null
val channels = when {
userIdNotNull -> getChannelsByUserId(userId)
userIdAndChannelTypeNotNull -> channelRepository.findByMemberIdAndChannelType(userId!!, channelType!!)
.ifEmpty { throw NotFoundException() }
primaryMentorIdNotNull -> channelRepository.findByPrimaryMentorId(primaryMentorId)
.ifEmpty { throw NotFoundException() }
primaryClinicianIdNotNull -> channelRepository.findByPrimaryClinicianId(primaryClinicianId)
.ifEmpty { throw NotFoundException() }
else -> throw InvalidRequestParameterException("This combination of request parameters is not supported")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论