Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.2K
Visualizações
Unsupported expression: Non-overridable members (here: ) may not be used in setup / verification expressions

I am using xUnit and Moq for writing test cases. I am using the code below to create a Mock but am getting the highlighted issue.

Picture of code and error pasted below

CloudBlockBlob source = null;
AccessCondition sourceAccessCondition = null;
AccessCondition destAccessCondition = null;
BlobRequestOptions options = null;
OperationContext operationContext = null;
CloudBlobContainer container = new CloudBlobContainer(uri);
Task task = null;
DeleteSnapshotOption deleteSnapshotOption = new DeleteSnapshotOption();

var mockCloudBlobClient = new Mock<Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient>();
mockCloudBlobClient.Setup(repo => repo.GetContainerReference("sample")).Returns(container);

var mockCloudBlobContainer = new Mock<Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer>(uri);
mockCloudBlobContainer.Setup(repo => repo.GetBlockBlobReference("sample")).Returns(new CloudBlockBlob(uri));

var mockBlobServiceProvider = new Mock<BlobServiceProvider>();
mockBlobServiceProvider.Setup(repo => repo.GetBlockBlobContainer("sample")).Returns(new CloudBloblContainer(new Uri("http://mytest")));

The line mockBlobServiceProvider.Setup(repo => repo.GetBlockBlobContainer("sample")).Returns(new CloudBloblContainer(new Uri("http://mytest"))); is giving me this error:

System.NotSupportedException: 'Unsupported expression: repo => repo.GetBlockBlobContainer("sample")

Non-overridable members (here: BlobServiceProvider.GetBlockBlobContainer) may not be used in setup / verification expressions.'

Class BlobServiceProvider:

Public Class BlobServiceProvider
{
    public CloudBlobContainer GetBlockBlobContainer(string containerName)
    {
        CloudBlobContainer Container = blobClient.GetContainerReference(containerName);
        Container.CreateIfNotExistsAsync();
        return Container;
    }
}

How can I resolve this issue?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda