Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Mocking in OData Client. #1695

Merged

Conversation

ElizabethOkerio
Copy link
Contributor

@ElizabethOkerio ElizabethOkerio commented Mar 9, 2020

Issues

This pull request fixes issue #1250.

Description

The DataServiceContext, DataServiceQuery, DataServiceQueryOfT and DataServiceQuerySingleOfT classes cannot be mocked as they are. In this PR, I have made these classes' public methods and properties virtual to support mocking.
Virtual methods can be mocked using any Mock Framework. Example, using the Moq Framework
You can mock the AddObject() method in the Microsoft.OData.Client.DataServiceContext class like below:

using Moq; 
using Xunit
namespace SampleMock
{
   /// How to mock the DataServiceContext class methods using moq framework example. 
    public class DataServiceContextMockTests 
    { 
        [Fact] 
        public void VerifyIfDataServiceContextAddObjectMethodIsMockable() 
        { 
            string str = "user"; 
            Mock mockObject = new Mock(); 
            mockObject.CallBase = true; 
            mockObject.Setup(x => x.AddObject(It.IsAny(), It.IsAny())).Callback(() => { str = "Saved Successfully"; }); 
            Users users = new Users(); 
            string result = users.SaveUser(mockObject.Object); 
            Assert.Equal(str, result); 
        }  
        /// A sample implementation class with methods calling the DataserviceContext.cs methods 
        public class Users 
        { 
            /// A simple method to save a user's details  
            /// A concrete class in the Microsoft.OData.Client library with implemented methods that this method calls.         
            public string SaveUser(DataServiceContext dataServiceContext) 
            { 
                string str = "user"; 
                object obj = null; 
                dataServiceContext.AddObject(str, obj); 
                return "Saved Successfully"; 
            } 
        } 
    }

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.

Copy link
Contributor

@paulodero paulodero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but a few minor issues to be addressed.

@ElizabethOkerio ElizabethOkerio force-pushed the supportmocking-1250-InODataClient branch from 29e1e06 to dc67110 Compare March 10, 2020 13:17
@ElizabethOkerio ElizabethOkerio added the Ready for review Use this label if a pull request is ready to be reviewed label Mar 10, 2020
@ElizabethOkerio ElizabethOkerio force-pushed the supportmocking-1250-InODataClient branch from 7aa98fe to 6884d40 Compare March 11, 2020 05:47
Copy link
Member

@g2mula g2mula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ElizabethOkerio ElizabethOkerio force-pushed the supportmocking-1250-InODataClient branch 3 times, most recently from c41e717 to bbf1ac1 Compare May 12, 2020 09:15
@KanishManuja-MS KanishManuja-MS added this to the 7.7.0 milestone May 18, 2020
@ElizabethOkerio ElizabethOkerio force-pushed the supportmocking-1250-InODataClient branch from bbf1ac1 to efbef33 Compare May 20, 2020 09:28
@ElizabethOkerio ElizabethOkerio force-pushed the supportmocking-1250-InODataClient branch from efbef33 to f186fba Compare May 20, 2020 11:32
@KanishManuja-MS KanishManuja-MS merged commit 787d083 into OData:master May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-needed Ready for review Use this label if a pull request is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants