This is a migrated thread and some comments may be shown as answers.

Unit Testing using just mock

5 Answers 83 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Surya
Top achievements
Rank 1
Surya asked on 22 Dec 2019, 12:12 PM

Hi ,

I want to write a unit test for a small class where it has following execution paths

 private bool? DetermineCheckState()
        {
            try
            {
               

                //1.where  ProjectsOperationData is a ObservableCollection<ProjectOperationData>{get;set;}

               //2 IsSelected is a public property in ProjectOperationData Class
                bool allChildrenChecked = ProjectsOperationData.Count(x => x.IsSelected == true) == ProjectsOperationData.Count;
                if (allChildrenChecked)
                {
                    return true;
                }

                //2. returns false if any project is not selected in Project Operations Form
                bool allChildrenUnChecked = ProjectsOperationData.Count(x => x.IsSelected == false) == ProjectsOperationData.Count;
                if (allChildrenUnChecked)
                {
                    return false;
                }
            }
            }

            return null;
        }

 

Can anyone suggest a way to write unit test?

5 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 25 Dec 2019, 12:08 PM

Hello Surya,

Thank you for contacting us. Due to the Christmas holidays, we are currently working with limited capacity and thus, we will need some additional time in order to address your inquiry correctly. We will get back to you as soon as possible.

Thank you for your understanding. 

Regards,
Dinko
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Surya
Top achievements
Rank 1
answered on 25 Dec 2019, 12:12 PM
Sure Dinko . I will wait until i get a solution.
0
Martin Ivanov
Telerik team
answered on 30 Dec 2019, 11:40 AM

Hello Surya,

We will get back to you within the next few working days (next Monday at the latest).

Regards,
Martin Ivanov
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Surya
Top achievements
Rank 1
answered on 06 Jan 2020, 08:15 AM
Okay Martin
0
Ivo
Telerik team
answered on 06 Jan 2020, 10:25 AM

Hello Surya,

The basic unit test scenarios which cover the main use cases could look like the ones below (SampleClass is the target class under test, please replace with your own):

[TestMethod]
public void TestChecked()
{
    // Arrange
    var sut = new SampleClass();
    Mock.Arrange(() => sut.ProjectsOperationData)
        .ReturnsCollection(
            new ObservableCollection<ProjectOperationData>()
            {
                new ProjectOperationData() { IsSelected = true },
                new ProjectOperationData() { IsSelected = true },
                new ProjectOperationData() { IsSelected = true },
                new ProjectOperationData() { IsSelected = true },
                new ProjectOperationData() { IsSelected = true },
            })
        .MustBeCalled();

    // Act
    var sutAccessor = new PrivateAccessor(sut);
    bool? result = (bool?)sutAccessor.CallMethod("DetermineCheckState");

    // Assert
    Mock.Assert(sut);
    Assert.IsTrue(result.HasValue && result.Value);
}

[TestMethod]
public void TestUnchecked()
{
    // Arrange
    var sut = new SampleClass();
    Mock.Arrange(() => sut.ProjectsOperationData)
        .ReturnsCollection(
            new ObservableCollection<ProjectOperationData>()
            {
                new ProjectOperationData() { IsSelected = false },
                new ProjectOperationData() { IsSelected = false },
                new ProjectOperationData() { IsSelected = false },
                new ProjectOperationData() { IsSelected = false },
                new ProjectOperationData() { IsSelected = false },
            })
        .MustBeCalled();

    // Act
    var sutAccessor = new PrivateAccessor(sut);
    bool? result = (bool?)sutAccessor.CallMethod("DetermineCheckState");

    // Assert
    Mock.Assert(sut);
    Assert.IsTrue(result.HasValue && !result.Value);
}

[TestMethod]
public void TestUndetermined()
{
    // Arrange
    var sut = new SampleClass();
    Mock.Arrange(() => sut.ProjectsOperationData)
        .ReturnsCollection(
            new ObservableCollection<ProjectOperationData>()
            {
                new ProjectOperationData() { IsSelected = true },
                new ProjectOperationData() { IsSelected = true },
                new ProjectOperationData() { IsSelected = true },
                new ProjectOperationData() { IsSelected = true },
                new ProjectOperationData() { IsSelected = false },
            })
        .MustBeCalled();

    // Act
    var sutAccessor = new PrivateAccessor(sut);
    bool? result = (bool?)sutAccessor.CallMethod("DetermineCheckState");

    // Assert
    Mock.Assert(sut);
    Assert.IsTrue(!result.HasValue);
}

I hope the provided information answers your questions. If you need any further assistance do not hesitate to write us back.

Regards,
Ivo
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
JustMock Free Edition
Asked by
Surya
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Surya
Top achievements
Rank 1
Martin Ivanov
Telerik team
Ivo
Telerik team
Share this question
or