![](/img/trans.png)
[英]Unit testing HttpResponseMessage with Autofac and Moq
[英]Autofac, unit-testing, C# - Difference between Autofac Moq and FakeItEasy integration package
任何人都可以帮助我区分Autofac Moq和FakeItEasy集成包吗? 我的演示项目使用Autofac作为其依赖项。 我想改变我的单元测试用例也使用Autofac Moq / FakeItEasy集成项目。 我目前正在使用Autofac MOQ
我指的是链接: http : //docs.autofac.org/en/latest/integration/moq.html有帮助,但我无法成功运行我的测试。
在下面的示例中,IDAL和IDATE是我的依赖项,IAGE是我的单元测试函数CalculateMatruity(age)的参数。
问题:
我的样本单元测试是:
[TestMethod]
public void TestGenericHelper()
{
//arrange
using (var mock = AutoMock.GetLoose())
{
mock.Mock<IDAL>().Setup(x => x.GetMaturityConfiguration()).Returns("0=Child|13=Teen|18=Adult");
mock.Mock<IAge>().Setup(x => x.Birthdate).Returns(DateTime.Parse("1987-06-16"));
mock.Mock<IDate>().Setup(x => x.Date).Returns(Convert.ToDateTime("2000-01-01"));
var sut = mock.Create<GenericHelper>();
String expected = "Teen";
Person age = new Person();
age.Birthdate = DateTime.Parse("1987-06-16");
//act
// mock.Provide<IGenericHelper, GenericHelper>(new NamedParameter("AgeObj", DateTime.Parse("1987-06-16")));
String actual = sut.CalculateMatruity(age);
//assert
mock.Mock<IDAL>().Verify(x => x.GetMaturityConfiguration());
mock.Mock<IAge>().Verify(x => x.Birthdate);
mock.Mock<IDate>().Verify(x => x.Date);
Assert.AreEqual(expected, actual);
}
}
以前我的测试是:
[TestMethod]
public void TestGenericHelper()
{
Person age = new Person();
age.Birthdate = DateTime.Parse("1987-06-16");
String expected = "Teen";
DateTime FakeDate = Convert.ToDateTime("2000-01-01");
String fake = "0=Child|13=Teen|18=Adult";
FakeDate DateHelperObj = new FakeDate(FakeDate);
FakeAgeDAL fakeObj = new FakeAgeDAL(fake);
GenericHelper GenericHelperObj = new GenericHelper(fakeObj, DateHelperObj);
String actual = GenericHelperObj.CalculateMatruity(age);
Assert.AreEqual(expected, actual);
}
Function GenericHelper looks like below:
public class GenericHelper : IGenericHelper
{
private IDAL _DAL;
private IDate _Date;
public GenericHelper(IDAL DAL, IDate Date)
{
_DAL = DAL;
_Date = Date;
}
public GenericHelper()
{ }
public String CalculateMatruity(IAge AgeObj)
{
String Maturity = "", AgeConfiguration;
//DateTime Now = DateTime.Now;
IDAL dalObj = _DAL;
IDate dateObj = _Date;
/*start */
//using (var container = UATContainer.Container.BeginLifetimeScope())
//{
AgeConfiguration = dalObj.GetMaturityConfiguration();
Int64 Age = dateObj.Date.Year - AgeObj.Birthdate.Year;
//AgeConfiguration = container.Resolve<IDAL>().GetMaturityConfiguration();
//var _d = container.Resolve<IDate>().Date.Year;
//Int64 Age = _d - AgeObj.Birthdate.Year; /* Using autofac approach */
var config = AgeConfiguration.Split('|').Select(s => s.Split('=')).ToDictionary(c => c[1], c => int.Parse(c[0]));
if (Age >= config["Child"] && Age < config["Teen"])
Maturity = "Child";
else if (Age >= config["Teen"] && Age < config["Adult"])
Maturity = "Teen";
else if (Age >= config["Adult"])
Maturity = "Adult";
else
Maturity = "Inapproriate to calculate age,verify the DOB";
//}
/*end */
return Maturity;
}
}
}
正确的测试结果:
public void TestGenericHelperAutofacFakeItEasy()
{
using (var fake = new AutoFake())
{
A.CallTo(() => fake.Resolve<IDAL>().GetMaturityConfiguration()).Returns("0=Child|13=Teen|18=Adult");
A.CallTo(() => fake.Resolve<IDate>().Date).Returns(Convert.ToDateTime("2000-01-01"));
var sut = fake.Resolve<GenericHelper>();
String expected = "Teen";
Person age = new Person();
age.Birthdate = DateTime.Parse("1987-06-16");
String actual = sut.CalculateMatruity(age);
A.CallTo(() => fake.Resolve<IDAL>().GetMaturityConfiguration()).MustHaveHappened();
A.CallTo(() => fake.Resolve<IDate>().Date).MustHaveHappened();
Assert.AreEqual(expected, actual);
}
}
更新 :使用您的测试和类,我看到的唯一问题是您的创建和Verify
IAge
。 不使用或不需要该变量。 你从未说过你的测试失败了。 它会失败吗?
Test 'AutofacMoq.Class1.TestGenericHelper' failed: Moq.MockException :
Expected invocation on the mock at least once, but was never performed: x => x.Birthdate
No setups configured.
No invocations performed.
? 如果是这样,您需要做的就是删除mock.Mock<IAge>
设置和验证。
我没有使用MSTest,我没试过这个,但是我相信Dror Helper的评估,并且会看看他的帖子MSTest V2 - 第一印象 ,其中他描述了一种方法,你可以编写一个看起来像
[DataTestMethod]
[DataRow(1, 2, 3)]
[DataRow(2, 2, 4)]
[DataRow(3, 2, 6)]
[DataRow(5, 2, 7)]
public void RowTest(int a, int b, int result)
{
Assert.AreEqual(result, a + b);
}
您似乎正确地填充了Person
对象上的属性,但这实际上取决于您的Person
类的定义方式。 我做了一个猜测,你可以在上面看到。
我没有使用,但据我所知,Autofac.Extras.Moq和Autofac.Extras.FakeItEasy的不同之处在于它们使用不同的模拟框架来提供虚假对象。 就个人而言,我会尝试使用Autofac.Extras.FakeItEasy,但那是因为我对FakeItEasy的熟悉。 我没有理由相信一种解决方案比另一种更好。
出于好奇,我使用AutoFaq.Extra.FakeItEasy重新编写测试,它就像这样,并且也通过了,我使用了上面使用的相同的辅助类和接口:
using (var fake = new AutoFake())
{
A.CallTo(() => fake.Resolve<IDAL>().GetMaturityConfiguration()).Returns("0=Child|13=Teen|18=Adult");
A.CallTo(() => fake.Resolve<IDate>().Date).Returns(Convert.ToDateTime("2000-01-01"));
var sut = fake.Resolve<GenericHelper>();
String expected = "Teen";
Person age = new Person();
age.Birthdate = DateTime.Parse("1987-06-16");
String actual = sut.CalculateMatruity(age);
//assert
A.CallTo(() => fake.Resolve<IDAL>().GetMaturityConfiguration()).MustHaveHappened();
A.CallTo(() => fake.Resolve<IDate>().Date).MustHaveHappened();
Assert.Equal(expected, actual);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.