Can I moq carthelper class?


Class seems to obsolete now, but we are using it in lot of places in our projects. I can't see any way to write unit test cases to the methods containing carthelper implementation. It seems be connecting to dbcontext. Is there a way?

Edited, Dec 14, 2021 15:33

Hi Dhanush,

To moq CartHelper, you would need to create a Cart Helper wrapping class with a custom interface.

Note that CartHelper is not support when you upgrade to Commerce 13 so you would have to discard the unit tests you are writing now when you upgrade. Consider upgrading before writing the unit tests if possible.

Dec 14, 2021 16:59
Johnny Mullaney - Dec 15, 2021 10:28
If you'd like to proceed with unit testing the cart helper, there is a good blog post on the wrapper approach here:

Let me know how you go with this

I strongly suggest to take a look at this Quicksilver/CartService.cs at master · episerver/Quicksilver · GitHub and see if you can convert your code. Beside making your code more futureproof, it also means you get your unit tests with ease

Dec 15, 2021 13:21
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.