调用方法的FakeItEasy返回对象

时间:2017-09-26 10:23:27

标签: c# .net unit-testing mocking fakeiteasy

我想像这样设置我的假货:

A.CallTo(() => this.repository.Create(A<PersonModel>._)).Returns(XYZ);

其中XYZA<PersonModel>._

中插入的变量相同

所以如果使用Create调用mySamplePersonModel,我希望该方法返回mySamplePersonModel

我怎样才能做到这一点?

提前致谢

2 个答案:

答案 0 :(得分:2)

我找到了你可以捕获这样的论点的答案:

A.CallTo(() => this.repository.Create(A<PersonModel>._)).ReturnsLazily(x => x.Arguments.Get<PersonModel>(0));

你甚至可以像这样修改这个参数:

A.CallTo(() => this.repository.Create(A<PersonModel>._)).ReturnsLazily(x =>
            {
                var personModel = x.Arguments.Get<PersonModel>(0);
                personModel.Name = "aName";
                return personModel;
            });

如果有人有更优雅的解决方案,请随意发布: - )

答案 1 :(得分:2)

您找到的解决方案是正确的。还有一个更具可读性的IMO:

bar <- c(26, 21, 16,4,10,5,6,13,3,2,5,3,1,3,7,22,7,0,0,0,0,4,1,0,3,0,0,0)
lab <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17","SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37")
pct <- round(bar/sum(bar)*100)
lab <- paste(lab,"%", sep = "")
lab <- paste(lab, pct)
lab0 <- c("SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37", "SN_02", "SN_03", "SN_04", "SN_05", "SN_09", "SN_10", "SN_17", "SN_19", "SN_20", "SN_31", "SN_32", "SN_33", "SN_34", "SN_37")
nlab <- length(lab)
type <- rep("Uniprot:MACKI", each=nlab/2)
library(ggplot2)
theme_set(theme_bw())
dd <- data.frame(lab0, type, bar, pct)
dd$lab0 <- reorder(dd$lab0,-dd$bar)
ggplot(dd,aes(x=lab0,y=bar,fill=lab0))+geom_bar(aes(alpha=factor(bar)), stat = "identity",
position=position_dodge(width=1))+scale_alpha_discrete(range=c(0.5,1.0))+
geom_text(aes(label=paste0(pct,"%"),group=interaction(lab0,type)),hjust=-0.5, position=position_dodge(width=1))
+ coord_flip()+expand_limits(y=20)+labs(x="",y="total")