我有为单元测试准备的以下方法,我知道它将始终为每个循环运行,有没有办法摆脱第二个return语句?
public Enums.GYRStatus GetStatusForTransformer(
string factoryCode,
Enums.Technology technology,
string transformerType,
int transformerSize,
string transformerModel)
{
fakeStandardsAndSizesFictionary = new Dictionary<Tuple<string,
Enums.Technology,
string, int, string>, int>()
{
{ Tuple.Create("SELUD", Technology.CVT,"---", 0, ""), 1} };
}
foreach (var pair in fakeStandardsAndSizesFictionary)
{
if (pair.Key.Item1 == factoryCode &&
pair.Key.Item2 == technology &&
pair.Key.Item3 == transformerType &&
pair.Key.Item4 == transformerSize &&
pair.Key.Item5 == transformerModel)
return (Enums.GYRStatus)pair.Value;
}
return (Enums.GYRStatus)1; // second return never used
}
答案 0 :(得分:7)
您可以替换
return (Enums.GYRStatus)1;
与
throw new InvalidOperationException();
假设永远不会到达这个地方,它在语义上看起来也更正确。
答案 1 :(得分:2)
假设(GYRStatus)1
是有效的返回值,您可以这样做:
GYRStatus status = GYRStatus.First;
foreach (var pair in fakeStandardsAndSizesFictionary)
{
if (pair.Key.Item1 == factoryCode &&
pair.Key.Item2 == technology &&
pair.Key.Item3 == transformerType &&
pair.Key.Item4 == transformerSize &&
pair.Key.Item5 == transformerModel)
{
status = (Enums.GYRStatus)pair.Value;
break;
}
}
return status;
您还可以返回GYRStatus?
以表示未选择任何值:
GYRStatus? status = null;
foreach (var pair in fakeStandardsAndSizesFictionary)
{
if (pair.Key.Item1 == factoryCode &&
pair.Key.Item2 == technology &&
pair.Key.Item3 == transformerType &&
pair.Key.Item4 == transformerSize &&
pair.Key.Item5 == transformerModel)
{
status = (Enums.GYRStatus)pair.Value;
break;
}
}
return status;
或者,您的枚举中也可以包含GYRStatus.None
值。这一切都取决于执行的流程。如果有可能最终找不到谓词的值,则返回默认值而不是抛出异常。如果不可能,请抛出。
答案 2 :(得分:2)
你可以这样做:
public Enums.GYRStatus GetStatusForTransformer(string factoryCode, Enums.Technology technology, string transformerType, int transformerSize, string transformerModel)
{
fakeStandardsAndSizesFictionary = new Dictionary<Tuple<string, Enums.Technology, string, int, string>, int>()
{
{Tuple.Create("SELUD",Technology.CVT,"---",0 ,""),1},
};
return fakeStandardsAndSizesFictionary
.Where(pair =>
pair.Key.Item1 == factoryCode
&& pair.Key.Item2 == technology
&& pair.Key.Item3 == transformerType
&& pair.Key.Item4 == transformerSize
&& pair.Key.Item5 == transformerModel)
.Select(pair => (Enums.GYRStatus)pair.Value)
.First();
}
答案 3 :(得分:0)
没有,即使你知道它将永远返回,编译器也不会!
答案 4 :(得分:0)
编译器无法知道循环ALLWAYS返回一个值。因此你必须提供那个开关。
答案 5 :(得分:0)
public Enums.GYRStatus GetStatusForTransformer(
string factoryCode,
Enums.Technology technology,
string transformerType,
int transformerSize,
string transformerModel)
{
Enums.GYRStatus retValue=(Enums.GYRStatus)1;
fakeStandardsAndSizesFictionary = new Dictionary<Tuple<string,
Enums.Technology,
string, int, string>, int>()
{
{ Tuple.Create("SELUD", Technology.CVT,"---", 0, ""), 1} };
}
foreach (var pair in fakeStandardsAndSizesFictionary)
{
if (pair.Key.Item1 == factoryCode &&
pair.Key.Item2 == technology &&
pair.Key.Item3 == transformerType &&
pair.Key.Item4 == transformerSize &&
pair.Key.Item5 == transformerModel)
retValue = (Enums.GYRStatus)pair.Value;
}
return retValue;
}