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

CODE RUB: Replace Input variables in Exception Tests with Some Variable #954

Open
hassanhabib opened this issue Jan 25, 2022 · 8 comments
Open
Labels

Comments

@hassanhabib
Copy link
Owner

hassanhabib commented Jan 25, 2022

In any test that is for RetrieveById under the exceptions section -

For example: CalendarServiceTests.Exceptions.RetrieveById.cs
You will find code in the tests in that file that looks like this:

[Fact]
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid randomCalendarId = Guid.NewGuid();
            Guid inputCalendarId = randomCalendarId;
            var sqlException = GetSqlException();

            var expectedCalendarDependencyException =
                new CalendarDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                broker.SelectCalendarByIdAsync(inputCalendarId))
                    .ThrowsAsync(sqlException);

            // when
            ValueTask<Calendar> retrieveCalendarByIdTask =
                this.calendarService.RetrieveCalendarByIdAsync(inputCalendarId);

            // then
            await Assert.ThrowsAsync<CalendarDependencyException>(() =>
                retrieveCalendarByIdTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                broker.LogCritical(It.Is(SameExceptionAs(expectedCalendarDependencyException))),
                    Times.Once);

            this.storageBrokerMock.Verify(broker =>
                broker.SelectCalendarByIdAsync(inputCalendarId),
                    Times.Once);

            this.dateTimeBrokerMock.Verify(broker =>
                broker.GetCurrentDateTime(),
                    Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }

What we need to do is two things:
Replace all input... variable names with some ... variable names. For instance, in here we have a variable called inputCalendarId we need to replace that with someCalendarId.

After we do the change the test method should look as follows:

        [Fact]
        public async Task ShouldThrowDependencyExceptionOnRetrieveWhenSqlExceptionOccursAndLogItAsync()
        {
            // given
            Guid someCalendarId = Guid.NewGuid();
            var sqlException = GetSqlException();

            var expectedCalendarDependencyException =
                new CalendarDependencyException(sqlException);

            this.storageBrokerMock.Setup(broker =>
                broker.SelectCalendarByIdAsync(someCalendarId))
                    .ThrowsAsync(sqlException);

            // when
            ValueTask<Calendar> retrieveCalendarByIdTask =
                this.calendarService.RetrieveCalendarByIdAsync(someCalendarId);

            // then
            await Assert.ThrowsAsync<CalendarDependencyException>(() =>
                retrieveCalendarByIdTask.AsTask());

            this.loggingBrokerMock.Verify(broker =>
                broker.LogCritical(It.Is(SameExceptionAs(expectedCalendarDependencyException))),
                    Times.Once);

            this.storageBrokerMock.Verify(broker =>
                broker.SelectCalendarByIdAsync(someCalendarId),
                    Times.Once);

            this.dateTimeBrokerMock.Verify(broker =>
                broker.GetCurrentDateTime(),
                    Times.Never);

            this.dateTimeBrokerMock.VerifyNoOtherCalls();
            this.loggingBrokerMock.VerifyNoOtherCalls();
            this.storageBrokerMock.VerifyNoOtherCalls();
        }

This issue and all related PRs are assigned to @KFitz114

@tharaniannamalai
Copy link
Contributor

@hassan, If this is still pending can i also contribute to do it.

@hassan
Copy link

hassan commented Mar 12, 2022

@hassan, If this is still pending can i also contribute to do it.

? I have no idea what this is about or why I was tagged here.

@ShriHumrudha
Copy link
Collaborator

@hassan sorry for the trouble.

@ShriHumrudha
Copy link
Collaborator

@hassan, If this is still pending can i also contribute to do it.

@tharanisantosh , Hassan's tag is this @hassanhabib

@ShriHumrudha
Copy link
Collaborator

@tharanisantosh Go for it

@tharaniannamalai
Copy link
Contributor

@tharanisantosh Go for it

Thanks Humrudha.

@kandarpgautam
Copy link

kandarpgautam commented Oct 14, 2023

this issue needs to be closed. It is already accomplished in the commit 8b4d7fa

@kandarpgautam
Copy link

kandarpgautam commented Feb 12, 2024

Please ignore the mention. I accidentally mentioned this issue while creating a Pull Request for some other issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants