or
var cmd = Mock.CreateLike<System.Data.IDbCommand>(c => c.CommandText == "dbo.GetStartTime");Mock.Arrange(() => cmd.ExecuteScalar()) .Returns("10:00");private static string LoadFromDatabase(DateTime date){ // create EntLib database object using (DbCommand cmd = db.GetStoredProcCommand("dbo.GetStartTime")) { db.AddInParameter(cmd, "DateToCheck", DbType.Date, date.Date); var startTime = db.ExecuteScalar(cmd); if (startTime == null || startTime == DBNull.Value) return null; else return startTime; }}var color = Mock.Create<Color>(Constructor.Mocked, Behavior.Loose);Mock.Arrange(() => Color.FromArgb(Arg.IsAny<byte>(), Arg.IsAny<byte>(), Arg.IsAny<byte>(), Arg.IsAny<byte>())).IgnoreArguments().IgnoreInstance().Returns(color);
var cache = Mock.Create<ICache>();
_cacheFactoryMock.Arrange(m => m.Application).Returns(cache);
cache.Arrange(m => m.GetOrAdd(Arg.AnyString, Arg.IsAny<Func<Dictionary<string, string>>>, Arg.IsAny<DateTime>(), Arg.IsAny<TimeSpan>(), null)).Throws(new DivideByZeroException());
Here is the invocation:return _caches.Application.GetOrAdd( string.Format(CacheKeyFormat, groupName), () => _ldapService.FindLdapUserEmailsInGroup(groupName), DateTime.Now.AddDays(1), Cache.NoSlidingExpiration );Dictionary<string, string> FindLdapUserEmailsInGroup(string groupName);public interface ICacheFactory{ ICache Application { get; } }public interface ICache{ TItem GetOrAdd<TItem, TState>(string key, Func<TState, TItem> getValue, TState state, DateTime absoluteExpiration, TimeSpan slidingExpiration, object dependencies = default(object)); TItem GetOrAdd<TItem>(string key, Func<TItem> getValue, DateTime absoluteExpiration, TimeSpan slidingExpiration, object dependencies = default(object));}public class PriceSeries{ private double _avPrice = 650; private double _relativePriceVol = 0.025; private double _priceRegress = 0.2; private double _currPrice = 650; private readonly Random _oracle = new Random(); public double AvPrice { get { return _avPrice; } private set { if(value > 0) {_avPrice = value;} } } public double CurrPrice { get { return _currPrice; } private set { if (value > 0) { _currPrice = value; } } } public double RelativePriceVol { get { return _relativePriceVol; } private set { if(value > 0 && value < 1) { _relativePriceVol = value; } } } public double PriceRegress { get { return _priceRegress; } private set { if (value > 0 && value < 1) { _priceRegress = value; } } } /// <summary> /// Return NdX /// </summary> /// <param name="numdice">Number of dice being rolled</param> /// <param name="numfaces">Highest numbered face on die</param> /// <returns></returns> internal int[] RollDice(int numdice, int numfaces) { numdice = Math.Max(1, numdice); numfaces = Math.Max(1, numfaces); int[] dice = new int[numdice]; for(int i=0;i<numdice;i++) { dice[i] = RollOneDie(numfaces); } return dice; } public double GetNextPrice() { int[] dice = RollDice(4, 6); int sum = -14; foreach(int t in dice) { sum += t; } double result = this.CurrPrice + this.PriceRegress*(this.AvPrice - this.CurrPrice)
+ this.AvPrice*this.RelativePriceVol*sum; return result; } internal int RollOneDie(int numfaces) { return _oracle.Next(numfaces) + 1; }}[TestMethod]public void TestFirstPriceGen(){ PriceSeries target = new PriceSeries(); // mock out dice roll int[] mockroll = new int[] {3,5,4,5}; Mock.Arrange(() => target.RollDice(4, 6)).Returns(mockroll); double expectedPrice = 698.75; // dice roll of +3 from initialisation double actualPrice = target.GetNextPrice(); Assert.AreEqual(expectedPrice,actualPrice);
Mock.Assert(() => RollDice(4,6),Occurs.Once());
}[TestMethod]public void TestRollDice(){ PriceSeries target = new PriceSeries(); Mock.Arrange(() => RollOneDie(6)).Returns(3); int[] expectedd6 = {3}; int[] actuald6 = target.RollDice(1, 6); Mock.Assert(() => RollOneDie(6), Occurs.Once()); Assert.AreEqual(expectedd6.Length,actuald6.Length); Assert.AreEqual(expectedd6[0],actuald6[0]); }public class MyClass{ private readonly MockClass generator; public MyClass(MockClass generator) { this.generator = generator; } public void Foo(Object someData) { generator.Bar(); }}public interface MockClass{ void Bar();} public class TestClass{ private readonly MockClass generator; private readonly MyClass myClass; public TestClass() { generator = Mock.Create<MockClass>(); myClass = new MyClass(generator); } [Fact] public void X64Failure() { myClass.Foo(null); Mock.Assert(() => generator.Bar()); }}X64Failure has failedOccurrence expectation failed. Expected calls in range [1, (any)]. Actual calls: 0 \x8\x2.\x19\x3.\x7 \x12\x1A\x3(String ) \x2.\x2 Assert(Nullable`1 \x2, Nullable`1 \x2, Int32 \x18\x3) Telerik.JustMock.Core.MocksRepository \x13\x2(\x2 \x2, Boolean \x11\x2, Occurs \x7\x2) Assert(Object \x2, Expression \x2, Occurs \x7\x2, Boolean \x11\x2) Telerik.JustMock.Mock.\x3 \x2() \x2.\x2 \x2(Action \x2) [TestClass] public class FooTest { [TestMethod] public void SimpleExampleWithDoNothing() { // Arrange var foo = Mock.Create<Foo>(); Mock.Arrange(() => foo.VoidCall()).CallOriginal(); Mock.Arrange(() => foo.AnotherCall()).DoNothing(); // Act foo.VoidCall(); // Assert Mock.Assert(foo); } } public interface IFoo { void VoidCall(); void AnotherCall(); } public class Foo : IFoo { public void VoidCall() { AnotherCall(); } public void AnotherCall() { throw new InvalidOperationException(); } }
public static class CacheHelper{ public static void AddCacheValue(string key, object obj, DateTime dt) { var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = dt; MemoryCache.Default.Set(key, obj, policy); }}

And then :IList<Dinner> FakeDinners()
to mock a DbSet. The problem is where I use " DbContext.DbSet<Dinner>.Find(2) " which gets from context or database the entity with primary key equal to 2. But in my test where I pass the FakeDinners() ,it doesn't work and return null. Looks like it doesn't know primary key inMock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners());FakeDinners() as it is list. So what is solution?