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

Unit tests #81

Open
vova3211 opened this issue Dec 24, 2020 · 2 comments
Open

Unit tests #81

vova3211 opened this issue Dec 24, 2020 · 2 comments
Assignees

Comments

@vova3211
Copy link
Collaborator

Repository does not have unit tests.

Also test projects are not the best way to test library. They good to be useful examples of library usage.

@vova3211 vova3211 self-assigned this Dec 24, 2020
gottscj added a commit to gottscj/AspNetCore.Identity.Mongo that referenced this issue Nov 4, 2021
fixes if DatabaseVersion == 4, migration 5 -> 6 will not run resulting in invalid schema.

adds build.yml github action which builds and runs test on PR's and push'es to master branch.

also addresses  matteofabbri#81
@jlg4 jlg4 mentioned this issue Nov 4, 2021
1 task
@jtlabs777
Copy link

jtlabs777 commented May 24, 2023

Speaking of test, I am unable to do unit or integrated tests on my controllers that use Idenitty Mongo as a dependency. Is there a way to mock Identity Mongo RolesManager and UserManagers classes? Or is there a away to spin up test containers on random ports and feed that port to Identity Mongo's connection string? Please advise.

@dave0292
Copy link

I'm having the same problem. I did it this way with no success:

_config = new ConfigurationBuilder()
   .AddJsonFile("appsettings.json")
   .SetBasePath(Directory.GetCurrentDirectory())
   .Build();

MongoDbConfig mongoDbSettings = _config.GetSection(nameof(MongoDbConfig)).Get<MongoDbConfig>();


client = new MongoClient(mongoDbSettings.ConnectionString);
var database = client.GetDatabase("MyDb");

IMongoCollection<AppUser> userCollection = database.GetCollection<AppUser>("Users");
IMongoCollection<AppRole> roleCollection = database.GetCollection<AppRole>("Roles");

IdentityErrorDescriber error = new IdentityErrorDescriber();
userStore = new UserStore<AppUser, AppRole, Guid>(userCollection, roleCollection, error);

userManager = new UserManager<AppUser>(userStore, null, null, null, null, null, null, null, null);

signInManager = new SignInManager<AppUser>(
   userManager, Mock.Of<IHttpContextAccessor>(), Mock.Of<IUserClaimsPrincipalFactory<AppUser>>(), null, null, null, null);

//Create user unit test method
bool success = false;

AppUser user = Activator.CreateInstance<AppUser>();
user.UserName = "user1";
user.Email = "[email protected]";

IdentityResult result = userManager.CreateAsync(user, "Password2023?").Result;

if(result == null)
{
   Assert.Fail("Error creating user user1");
}

But I receive a null result on the "CreateAsync" method. I thought that this was the way to make the userStore to point to the mongoDb, but I´m not sure if it is the correct way.

Anyone has the same issue?

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

No branches or pull requests

3 participants