Moq - Non-overridable members may not be used in setup / verification expressions
Moq creates an implementation of the mocked type. If the type is an interface, it creates a class that implements the interface. If the type is a class, it creates an inherited class, and the members of that inherited class call the base class. But in order to do that it has to override the members. If a class has members that can't be overridden (they aren't virtual, abstract) then Moq can't override them to add its own behaviors.
In this case there's no need to mock PagingOptions
because it's easy to use a real one. Instead of this:
var mockPagingOptions = new Mock<PagingOptions>();
mockPagingOptions.Setup(po => po.Limit).Returns(25);
mockPagingOptions.Setup(po => po.Offset).Returns(0);
Do this:
var pagingOptions = new PagingOptions { Limit = 25, Offset = 0 };
How do we determine whether or not to mock something? Generally speaking, we mock something if we don't want to include the concrete runtime implementation in our test. We want to test one class not both at the same time.
But in this case PagingOptions
is just a class that holds some data. There's really no point in mocking it. It's just as easy to use the real thing.
Non-overridable members may not be used in setup / verification expressions
From the second unit test you tried, when you create instance of SearchViewModel, there is no initialize of _productionOrderService.
if _productionOrderService is created in SearchViewModel it might not be initialized due to lack of their dependencies.
you have to provide _productionOrderService to the SearchViewModel by
- make it public and set it when create SearchViewModel
- make it private and pass it through constructor when create SearchViewModel
then you can mock _productionOrderService and setup GetListAllAsync() in unit test
Unsupported expression: Non-overridable members (here: ) may not be used in setup / verification expressions
The moq
use dynamic proxy to create wrapper around mocked type in order to be able to intercept calls and change the behaviour of the invoked methods.
How proxy is built?
- If you mocking
interface
proxy is just an implementation of the interface - If you have
class
the proxy is override of the class
Question: Could you mock sealed class
or non virtual
(abstract
) method?
You guess, no. There is no way to create wrapper around it using dynamic proxy. moq
itself suggesting the same, if you look at the error:
Non-overridable members (here:
BlobServiceProvider.GetBlockBlobContainer) may not be used in setup
To answer your question, either you need to extract the interface
out of your provider and mock it or mark the method as virtual. Personaly, I do prefer option with the interface
but it might requre some refactoring and DI.
Nunit Moq: Unsupported expression: Value Non-overridable members may not be used in setup / verification expressions
I'm unaware of any built-in DataGeneric
class, so I assume it is a user-defined class.
The error says
... Value Non-overridable members (here: DataGeneric.get_Value) ...
which means you should not mock the RiskId.Value
.
You should try to setup a mock for the RiskId
itself:
data.Setup(i => i.RiskId).Returns(new StringData("123"));
I've assumed your StringData
has a constructor which can receive its value.
Non-overridable members (here: Login.getInfo) may not be used in setup / verification expressions
Moq is constrained isolation framework. It can mock only virtual methods.
Extract interface from you Login
class.
public interface ILogin
{
List<string> GetInfo(string tableName, string[] columnNames);
// other members
}
Add this interface to the class definition.
public class Login : ILogin
{
public List<string> GetInfo(string tableName, string[] columnNames)
{
using (var command = new SqlCommand("SELECT * from [" + tableName + "]", connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
{
List<string> infoList = new List<string>();
while (reader.Read())
{
for (int i = 0; i < columnNames.Length; i++)
{
infoList.Add(reader[columnNames[i]].ToString());
}
}
return infoList;
}
}
}
}
Mock this interface instead of class.
var mock = new Mock<ILogin>();
Moq System.NotSupportedException
You have to make the property overridable, so make it virtual
:
public virtual int? ProductMaxLenght { get ...
C#: Moq : Message: System.NotSupportedException : Unsupported expression: item => item.RouteId Non-overridable members
As RouteId
is not virtual, you cannot mock it. But since ContextData
is a simple data class without behaviour, there is also no need to mock it. You can do simply:
ContextArbiter.Setup(p => p.GetContextData()).Returns(new ContextData { RouteId = "64" });
Related Topics
Read and Parse a Json File in C#
How to Remove Empty Lines from a Formatted String
How to Read Appsettings.Json With Array of Values
Searching If Value Exists in a List of Objects Using Linq
Convert Time With Milliseconds to Datetime in C#
Using X-Alt-Desc/Applying HTML to Calendar Invites in Outlook
Remove the Escape Sequence '\' from String to Convert It to Xmldocument
How to Map to a Dictionary Object from Database Results Using Dapper Dot Net
How to Ignore the Certificate Check When Ssl
How to Get Url Referrer in ASP.NET Core MVC
How to Dynamically Adjust Table Column Width in Rdlc Report
Asp.Net Core Form Post Results in a Http 415 Unsupported Media Type Response
Clearing a Textbox Leaves an Invisible Character
How to Pass Multiple Arguments in Processstartinfo
Asp.Net Core Get Json Array Using Iconfiguration
How to Convert HTML to Pdf Using Itextsharp