Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have got the following method in the controller.

C#
public async Task


Where CientId uses the following method in the same controller

C#
public Guid CientId
       {
           get
           {
               Guid clientIdParam;
               Guid.TryParse(Request.QueryString["clientId"], out clientIdParam);

               return clientIdParam;
           }
           set{ }
       }


Now I am trying to write the unit for the above controller method in which I want to mock the line model.ClientId = CientId

Till now I have tried the following test

C#
[Test]
       public async void Edit_edits_booking_form_settings()
       {
           //Arrange
           BookingFormSettingsController controller = Substitute.ForPartsOf


Can anyone please help me moch the line I mentioned .

What I have tried:

I tried using the partial substitute , automockcontroller
Posted
Updated 29-Sep-16 5:50am
Comments
F-ES Sitecore 29-Sep-16 11:35am    
We can't really see your code, update your question to improve it. However be prepared for disappointment as you don't mock individual lines, you mock whole classes.

1 solution

There isn't enough here to go one really, but one issue is that you are creating a dependency on Request.QueryString. Since this is MVC, why not add this as part of your model?

Also, another note about that is it looks like the code snippet of ClientId you mentioned is inside your controller. If that is so, you are breaking MVC pattern rules and should move that to your Model.

However, take a look at the following links. You might have to alter your approach to unit testing some in order to mock what you want but this should help at least.

how to mock request querystring - Google Search[^]

c# - How to set the value of a query string in test method Moq - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900