Unit tests

Nicolás P asked on March 29, 2016 04:42

Hi,

I created an unit testing project using fake info and InfoProvider objects, I managed to create a faked user using following code:

Fake<UserInfo, UserInfoProvider>().WithData(
    UserInfo.New(u =>
    {
        u.UserName = "Usertest";
    })
);

But now I need to set a role for that faked user, how can I perform that?

Recent Answers


Virgil Carroll answered on March 29, 2016 15:35

RoleInfo role = new RoleInfo(); role.RoleName = "[SOME NAME]"; role.SiteID = SiteContext.CurrentSiteID; RoleInfoProvider.SetRoleInfo(role);

UserRoleInfo userRole = new UserRoleInfo(); userRole.UserID = [Your User ID]; userRole.RoleID = [your Role ID]; UserRoleInfoProvider.SetUserRoleInfo(userRole);

So you are going to need to get the Role ID and User ID after you create them, so you can tie the user and role together. I am pretty sure the role relationship will not be able to be accomplished as a temporary, in-memory item, because this relationship is created in the database and not an in-memory item. Someone from Kentico might be able to give you more info...but you could just setup a clean up operation at the end to take care of any objects you created.

1 votesVote for this answer Mark as a Correct answer

Nicolás P answered on March 29, 2016 17:04

Thank you Virgil, I did it as you suggested but it didn't work, possibly is as you say, the relationship is not created in-memory. I think I need to move to Integration Tests, but I have a couple of questions there.

I was thinking that for Integration Tests I should create another site only for unit testing, so in this way If we made changes in the development site we can run staging synchronization to keep up to date the test site, am I right?

Regarding clean up at the end of tests execution, should I a create a method to remove created and updated items?

0 votesVote for this answer Mark as a Correct answer

Virgil Carroll answered on March 30, 2016 00:35

Nicolas, Integration testing is not my total expertise, but hopefully some others will put there thoughts in here...but here are my thoughts. I do not think its a bad idea for setting up a separate server for unit testing, but are you using version 9? If so, I would not think about staging but instead would implement the new continuous integration module which will give a much more accurate sync of changes and can be done in only a 1 way direction if you desired. Plus you have granular control (if you want) around what you want to sync, such as not user accounts.

As far as the 2nd part, my suggestion would be either as you create the users, you either store their ideas in an in-memory list of some sort so you could just delete out those records, or maybe create a 2nd table for that which you could query for all those values. Where you could just use the creation and updated dates, you run the risk of deleting things that were unexpected because of that.

Just my thoughts.

0 votesVote for this answer Mark as a Correct answer

Nicolás P answered on March 31, 2016 03:38

Thank you again Virgil, I will take into account your advice. I will upgrade the project to version 9 to take advantage of continuous integration.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.