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

Refactor tests to have less redundancy #253

Open
JonasMuehlmann opened this issue Jul 14, 2021 · 0 comments · May be fixed by #333
Open

Refactor tests to have less redundancy #253

JonasMuehlmann opened this issue Jul 14, 2021 · 0 comments · May be fixed by #333
Assignees
Labels
effort: high When you have a free weekend... priority: medium Nice to have... refactor Does not add new functionality, but alters existing ones status: not started tests

Comments

@JonasMuehlmann
Copy link
Owner

JonasMuehlmann commented Jul 14, 2021

Every test method has setup that is quite extensive in many and completely redundant in almost all cases.
To minimize the boilerplate code used for creating needed data, I am thinking about having global variables for the id's of common DB entries(maybe we can even save whole objects?) and provide a simple function to set up needed data.
Alternatively, we could add a [TestInitialize] attributed method to set up all available entries, reducing the number of code lines needed for setup to 0.

The setup function could create a dictionary.

A function like

        [TestMethod]
        public void ResolveMentionNoMention_Test()
        {
            var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Task.Run(async () =>
            {
                uint? teamId = await TeamService.CreateTeam(testName + "Team");
                Assert.IsNotNull(teamId);

                string userId = (await UserService.GetOrCreateApplicationUser(new User(){Id= testName + "UserId" ,DisplayName = testName + "UserName"})).Id;
                Assert.IsNotNull(userId);

                uint? channelId = await ChannelService.CreateChannel(testName + "Channel", teamId.Value);
                Assert.IsNotNull(channelId);

                uint? messageId = await MessageService.CreateMessage(channelId.Value, userId, testName + "Message");
                Assert.IsNotNull(messageId);

                uint? mentionId = await MentionService.CreateMention(MentionTarget.User, userId);
                Assert.IsNotNull(mentionId);

                var messageOriginal = (await MessageService.GetMessage(messageId.Value)).Content;
                Assert.AreEqual(testName + "Message", messageOriginal);

                var messageResolved = await MentionService.ResolveMentions(messageOriginal);
                Assert.AreEqual(messageOriginal, messageResolved);
            }).GetAwaiter().GetResult();
        }

could then be reduced to

        [TestMethod]
        public void ResolveMentionNoMention_Test()
        {
            var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            var data = Setup(Entity.User, Entity.Team, Entity.Channel, Entity.Message, Entity.Mention);

            Task.Run(async () =>
            {


                var messageOriginal = (await MessageService.GetMessage(messageId.Value)).Content;
                Assert.AreEqual(testName + "Message", messageOriginal);

                var messageResolved = await MentionService.ResolveMentions(messageOriginal);
                Assert.AreEqual(messageOriginal, messageResolved);
            }).GetAwaiter().GetResult();
        }

or like this, generating all data with defaults but executing a custom generator for the user:

        [TestMethod]
        public void ResolveMentionNoMention_Test()
        {
            var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            var data = Setup(username=() => {/* my custom user generator here */});

            Task.Run(async () =>
            {
                var messageOriginal = (await MessageService.GetMessage(messageId.Value)).Content;
                Assert.AreEqual(testName + "Message", messageOriginal);

                var messageResolved = await MentionService.ResolveMentions(messageOriginal);
                Assert.AreEqual(messageOriginal, messageResolved);
            }).GetAwaiter().GetResult();
        }

This improves readability greatly by refactoring out non-essential code and leaving behind the actual test scenario(Resolving a mention in this case).

@JonasMuehlmann JonasMuehlmann added status: not started refactor Does not add new functionality, but alters existing ones priority: medium Nice to have... effort: high When you have a free weekend... tests labels Jul 14, 2021
@JonasMuehlmann JonasMuehlmann self-assigned this Jul 14, 2021
@JonasMuehlmann JonasMuehlmann added this to the Improve code quality milestone Aug 11, 2021
@JonasMuehlmann JonasMuehlmann linked a pull request Aug 20, 2021 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: high When you have a free weekend... priority: medium Nice to have... refactor Does not add new functionality, but alters existing ones status: not started tests
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant