Skip to content
DataMiner DoJo

More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Search in posts
Search in pages
Search in posts
Search in pages
Log in
Menu
  • Blog
  • Questions
  • Learning
    • E-learning Courses
    • Open Classroom Training
    • Certification
      • DataMiner Fundamentals
      • DataMiner Configurator
      • DataMiner Automation
      • Scripts & Connectors Developer: HTTP Basics
      • Scripts & Connectors Developer: SNMP Basics
      • Visual Overview – Level 1
      • Verify a certificate
    • Tutorials
    • Video Library
    • Books We Like
    • >> Go to DataMiner Docs
  • Expert Center
    • Solutions & Use Cases
      • Solutions
      • Use Case Library
    • Markets & Industries
      • Media production
      • Government & defense
      • Content distribution
      • Service providers
      • Partners
      • OSS/BSS
    • DataMiner Insights
      • Security
      • Integration Studio
      • System Architecture
      • DataMiner Releases & Updates
      • DataMiner Apps
    • Agile
      • Agile Webspace
      • Everything Agile
        • The Agile Manifesto
        • Best Practices
        • Retro Recipes
      • Methodologies
        • The Scrum Framework
        • Kanban
        • Extreme Programming
      • Roles
        • The Product Owner
        • The Agile Coach
        • The Quality & UX Coach (QX)
    • DataMiner DevOps Professional Program
  • Downloads
  • More
    • Feature Suggestions
    • Climb the leaderboard!
    • Swag Shop
    • Contact
      • General Inquiries
      • DataMiner DevOps Support
      • Commercial Requests
    • Global Feedback Survey
  • PARTNERS
    • All Partners
    • Technology Partners
    • Strategic Partner Program
    • Deal Registration
  • >> Go to dataminer.services

Unit test extension method

Solved2.02K views15th November 2021Mock MOq Unit Tests
4
Maxim Bouckaert [SLC] [DevOps Member]169 9th November 2021 0 Comments

Hey Dojo-community,

I was working on some unit tests and was wondering what the prefered method for solving the following issue is:

imagine I have three extension methods:

public static bool ContainsElementSubscriptions(this SubscriptionSetInfoMessage message)
public static bool ContainsServiceSubscriptions(this SubscriptionSetInfoMessage message)
public static bool ContainsServiceAndElementSubscriptions(this SubscriptionSetInfoMessage message)

The last extension method uses the first two to return a result.
I would like to cover each of these methods in a test, but I don't want them to be dependant on each other, to make it clear which method exactly fails.

To do this, I did the following:

Mock<SubscriptionSetInfoMessage> mockedMessage = new Mock<SubscriptionSetInfoMessage>();
mockedMessage.CallBase = true;
mockedMessage.Setup(x => x.ContainsElementSubscriptions()).Returns(true);
mockedMessage.Setup(x => x.ContainsServiceSubscriptions()).Returns(true);

When writing the unit test for the last method, I got this error when trying to mock the first two methods:

Tests.QActionTests.ContainsServiceAndElementSubscriptionsTest threw exception:
System.NotSupportedException: Unsupported expression: x => x.ContainsElementSubscriptions()
Extension methods (here: QAction.ContainsElementSubscriptions) may not be used in setup / verification expressions.

How would you solve this?

Maxim Bouckaert [SLC] [DevOps Member] Selected answer as best 15th November 2021

3 Answers

  • Active
  • Voted
  • Newest
  • Oldest
1
Pedro Debevere [SLC] [DevOps Enabler]1.80K Posted 12th November 2021 1 Comment

Hi Maxim

Moq is a constrained framework, customizing the implementation of an extension method is not possible.

Like Jens suggests, to do what you are after, you'd need to use an unconstrained framework such as Moles, Pose or Microsoft Fakes (the latter is part of the Enterprise edition of Visual Studio). These allow customizing the implementation of methods that cannot be done using a constrained framework (using what is typically referred to as shims).

Now, while you could use an unconstrained framework to solve this, I'm not sure you should. The current test seems to be tightly coupled with implementation details: The fact that the method under test calls the other other extension methods is not relevant from an end user perspective. The test should test the observable behavior without considering the steps taken to arrive at the result (in this case, not consider the fact that the method under test calls the other extension methods). This also allows resistance to refactoring, one of the pillars of a good unit test.

Maxim Bouckaert [SLC] [DevOps Member] Posted new comment 15th November 2021
Maxim Bouckaert [SLC] [DevOps Member] commented 15th November 2021

Thank you for the explanation!

0
Michiel Oda [SLC] [DevOps Enabler]3.58K Posted 9th November 2021 1 Comment

Hi Maxim

You can just call the method directly via the 'static' way.

bool result  = ExtensionClassName.ContainsElementSubscriptions(mockedMessage.Object);

Just think of it as a regular static method.

Maxim Bouckaert [SLC] [DevOps Member] Posted new comment 9th November 2021
Maxim Bouckaert [SLC] [DevOps Member] commented 9th November 2021

Hey Michiel,

Thank you for your answer, but it seems that this does not solve the issue.
I can indeed call it using Qaction.methodname(object), but the failure seems to happen at the ‘setup’ part of the code. In that part, I can’t really use this trick.

0
Gellynck Jens [SLC]2.71K Posted 9th November 2021 0 Comments

There are probably better solutions but I came across a similar situation once and there I found you could use the Moles framework for mocking static and built-in .NET methods.

Gellynck Jens [SLC] Answered question 9th November 2021
Please login to be able to comment or post an answer.

My DevOps rank

DevOps Members get more insights on their profile page.

My user earnings

0 Dojo credits

Spend your credits in our swag shop.

0 Reputation points

Boost your reputation, climb the leaderboard.

Promo banner DataMiner DevOps Professiona Program
DataMiner Integration Studio (DIS)
Empower Katas

Recent questions

Alarm Dashboard PDF/CSV Export 0 Answers | 0 Votes
Is the Microsoft SharePoint Connector Still Usable 0 Answers | 0 Votes
Is the Microsoft SharePoint Connector Still Usable 0 Answers | 0 Votes

Question Tags

adl2099 (115) alarm (62) Alarm Console (82) alarms (100) alarm template (83) Automation (223) automation scipt (111) Automation script (167) backup (71) Cassandra (180) Connector (108) Correlation (68) Cube (150) Dashboard (194) Dashboards (188) database (83) DataMiner Cube (57) DIS (81) DMS (71) DOM (139) driver (65) DVE (55) Elastic (83) Elasticsearch (115) elements (80) Failover (104) GQI (159) HTTP (76) IDP (74) LCA (151) low code app (166) low code apps (93) lowcodeapps (75) MySQL (53) protocol (203) QAction (83) security (88) services (51) SNMP (86) SRM (337) table (54) trending (87) upgrade (62) Visio (539) Visual Overview (345)
Privacy Policy • Terms & Conditions • Contact

© 2025 Skyline Communications. All rights reserved.

DOJO Q&A widget

Can't find what you need?

? Explore the Q&A DataMiner Docs

[ Placeholder content for popup link ] WordPress Download Manager - Best Download Management Plugin