asp.net MVC3 Unit Testing - Role Provider -
unit testing of role provider fail.
[testmethod] public void findusersinrole() { mock<iusersinrolerepository> userinrolemock = new mock<iusersinrolerepository>(); userinrolemock.setup(m => m.usersinroles).returns(new usersinrole[] { new usersinrole { userid = guid.parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), roleid = guid.parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") }, new usersinrole { userid = guid.parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), roleid = guid.parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") }, new usersinrole { userid = guid.parse("cccccccc-cccc-cccc-cccc-cccccccccccc"), roleid = guid.parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") }, new usersinrole { userid = guid.parse("dddddddd-dddd-dddd-dddd-dddddddddddd"), roleid = guid.parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") }, new usersinrole { userid = guid.parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"), roleid = guid.parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb") } }.asqueryable()); mock<irolerepository> rolemock = new mock<irolerepository>(); rolemock.setup(m => m.roles).returns(new role[] { new role { roleid = guid.parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), rolename = "test" }, new role { roleid = guid.parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), rolename = "admin" } }.asqueryable()); mock<iuserrepository> usermock = new mock<iuserrepository>(); usermock.setup(m => m.users).returns(new user[] { new user { userid = guid.parse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), useraccount = "abcdef" }, new user { userid = guid.parse("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"), useraccount = "bcdef" }, new user { userid = guid.parse("cccccccc-cccc-cccc-cccc-cccccccccccc"), useraccount = "cdef" }, new user { userid = guid.parse("dddddddd-dddd-dddd-dddd-dddddddddddd"), useraccount = "bcdf" }, new user { userid = guid.parse("eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"), useraccount = "abcde" } }.asqueryable()); roleprovider target = new roleprovider(usermock.object, rolemock.object, userinrolemock.object); string[] result = target.findusersinrole("test", "cde"); assert.areequal(result[0], "abcdef"); assert.areequal(result[1], "bcdef"); assert.areequal(result[2], "cdef"); }
unit test code
string[] result = target.findusersinrole("test", "cde"); <-- error
findusersinrole - gets array of user names in role user name contains specified user name match.
system.nullreferenceexception raised , try debug.
why nullreferenceexception?
ps - findusersinrole (roleprovider)
public override string[] findusersinrole(string rolename, string useraccounttomatch) { guid roleid = rolerepository.getrole(rolename).roleid; // roleid retrun.. nullreferenceexception var roleinusers = (from ru in usersinrolerepository.usersinroles ru.roleid == roleid select ru.userid).toarray(); var finduserresult = (from u in userrepository.users roleinusers.contains(u.userid) && u.useraccount.contains(useraccounttomatch) select u.useraccount).toarray(); return finduserresult; }
your cde not fake user in mackuseraccount . return null.
try below code string[] result = target.findusersinrole("test", "abcdef"); instead of
string[] result = target.findusersinrole("test", "cde");
Comments
Post a Comment