???Moq?????????????
???????????? ???????[ 2013/11/18 16:49:27 ] ????????
??????????????????????????????????????????????????????????????壺
namespace Moq
{
// Summary:
// Options to customize the behavior of the mock.
public enum MockBehavior
{
// Summary:
// Causes the mock to always throw an exception for invocations that don't have
// a corresponding setup.
Strict = 0??
//
// Summary:
// Will never throw exceptions?? returning default values when necessary (null
// for reference types?? zero for value types or empty enumerables and arrays).
Loose = 1??
//
// Summary:
// Default mock behavior?? which equals Moq.MockBehavior.Loose.
Default = 1??
}
}
|
??????????????????????
????var mock = new Mock<IFake>(MockBehavior.Strict);
?????????mock????????????а???????Setup?????????
????MockFactory
????????????????????????????????????????????????????????????????????????????±??????
var factory = new MockFactory(MockBehavior.Strict) { DefaultValue = DefaultValue.Mock };
// Create a mock using the factory settings
var fooMock = factory.Create<IFake>();
// Create a mock overriding the factory settings
var barMock = factory.Create<IEmailSender>(MockBehavior.Loose);
// Verify all verifiable expectations on all mocks created through the factory
factory.Verify();
|
??????????????????????????????????????????????????????÷?????????????????Щ???裺???...???????... ??
???????±????????????????????ЩMock<T> ???л?????????????????
????Verification
???????????????????????????????????????????????????????????Ρ??????????????????Verify()???????????±??????
????mock.Verify(foo => foo.DoSomething("Ping")?? Times.Once());
????????????????DoSomething("Ping")?????????????????????Ρ?????Once?????????и??????????????????????????????????????Ρ??磺 AtLeast?? AtLeastOnce?? AtMost?? AtMostOnce?? Between?? Equals?? Exactly?? Never?? ??Once
????????????????????????????????????????????±???????
public void Test_FindByName_GetCalled()
{
// create some mock data
IList<Product> products = new List<Product>
{
new Product { ProductId = 1?? Name = "C# Unleashed"??
Description = "Short description here"?? Price = 49.99 }??
new Product { ProductId = 2?? Name = "ASP.Net Unleashed"??
Description = "Short description here"?? Price = 59.99 }??
new Product { ProductId = 3?? Name = "Silverlight Unleashed"??
Description = "Short description here"?? Price = 29.99 }
};
Mock<IProductRepository> mock = new Mock<IProductRepository>();
//mock
//.Setup(sender => sender.FindById(It.IsAny<int>()))
//.Returns((int s) => products.Where(
// x => x.ProductId == s).Single());
mock.Object.FindById(1);
mock
.Verify(x => x.FindById(1)?? Times.Once());
}
}
|
???????????????????????????????
??????????mock.Object.FindById(1)??????????case??????б?ü?????????????mock.Object???????????????????????case????????????????????????????????????????????????????????????????????
?????????????????????????????????????????“????????”????????????????Setup ????????????????????
???????????????????????к?????Mock??????????????漰???????????????????????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????