Mocking EF DbContext with Moq

I managed to solve it by creating a FakeDbSet<T> class that implements IDbSet<T> public class FakeDbSet<T> : IDbSet<T> where T : class { ObservableCollection<T> _data; IQueryable _query; public FakeDbSet() { _data = new ObservableCollection<T>(); _query = _data.AsQueryable(); } public virtual T Find(params object[] keyValues) { throw new NotImplementedException(“Derive from FakeDbSet<T> and override Find”); } public … Read more

How to mock an async repository with Entity Framework Core

Thanks to @Nkosi for pointing me to a link with an example of doing the same thing in EF 6: https://msdn.microsoft.com/en-us/library/dn314429.aspx. This didn’t work exactly as-is with EF Core, but I was able to start with it and make modifications to get it working. Below are the test classes that I created to “mock” IAsyncQueryProvider: … Read more

How to MOQ an Indexed property

It’s not clear what you’re trying to do because you don’t show the declaration of the mock. Are you trying to mock a dictionary? MyContainer[(string s)] isn’t valid C#. This compiles: var mock = new Mock<IDictionary>(); mock.SetupGet( p => p[It.IsAny<string>()]).Returns(“foo”);

How to throw a SqlException when needed for mocking and unit testing?

I have a solution to this. I’m not sure whether it’s genius or madness. The following code will create a new SqlException: public SqlException MakeSqlException() { SqlException exception = null; try { SqlConnection conn = new SqlConnection(@”Data Source=.;Database=GUARANTEED_TO_FAIL;Connection Timeout=1″); conn.Open(); } catch(SqlException ex) { exception = ex; } return(exception); } which you can then use … Read more

Moq verify with object parameter

The first attempt is closer to what you want to achieve. The reason it fails is that Moq (probably) uses Object.Equals under the cover to test if the ImageFilterOptions parameter that the method was called with is the same instance as the one you supplied in the call to Verify. It is impossible for them … Read more

Moq: Invalid setup on a non-overridable member: x => x.GetByTitle(“asdf”)

In order to control the behavior of a mock object (in Moq, at least), you either need to mock an interface, or make sure that the behavior you’re trying to control is marked virtual. In your comment, I understand it so that the instantiating of _mockArticleDao is done something like this: _mockArticleDao = new Mock<ArticleDAO>(); … Read more

Can you help me understand Moq Callback?

Hard to beat https://github.com/Moq/moq4/wiki/Quickstart If that’s not clear enough, I’d call that a doc bug… EDIT: In response to your clarification… For each mocked method Setup you perform, you get to indicate things like: constraints on inputs the value for / way in which the return value (if there is one) is to be derived … Read more

How do I Moq a method that has an optional argument in its signature without explicitly specifying it or using an overload?

I believe your only choice right now is to explicitly include the bool parameter in the setup for Foo. I don’t think it defeats the purpose of specifying a default value. The default value is a convenience for calling code, but I think that you should be explicit in your tests. Say you could leave … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)