Multiple Unit Tests' Running With multiple Connections To Database
up vote
1
down vote
favorite
In the Asp.Net core project, there are several unit tests used services for connecting to the database and bring real data, so multiple concurrent connections are created. When these tests run, I received 'A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.' error, but I do not know how can I fix this error without using async ways.
asp.net testing model-view-controller core xunit
New contributor
add a comment |
up vote
1
down vote
favorite
In the Asp.Net core project, there are several unit tests used services for connecting to the database and bring real data, so multiple concurrent connections are created. When these tests run, I received 'A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.' error, but I do not know how can I fix this error without using async ways.
asp.net testing model-view-controller core xunit
New contributor
Are you sure you have a bug in your test setup and not a bug in the system under test?
– John Wu
yesterday
Yes, I am sure because I used this way for another project.
– Niloufar Mohammadi
yesterday
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In the Asp.Net core project, there are several unit tests used services for connecting to the database and bring real data, so multiple concurrent connections are created. When these tests run, I received 'A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.' error, but I do not know how can I fix this error without using async ways.
asp.net testing model-view-controller core xunit
New contributor
In the Asp.Net core project, there are several unit tests used services for connecting to the database and bring real data, so multiple concurrent connections are created. When these tests run, I received 'A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.' error, but I do not know how can I fix this error without using async ways.
asp.net testing model-view-controller core xunit
asp.net testing model-view-controller core xunit
New contributor
New contributor
New contributor
asked yesterday
Niloufar Mohammadi
61
61
New contributor
New contributor
Are you sure you have a bug in your test setup and not a bug in the system under test?
– John Wu
yesterday
Yes, I am sure because I used this way for another project.
– Niloufar Mohammadi
yesterday
add a comment |
Are you sure you have a bug in your test setup and not a bug in the system under test?
– John Wu
yesterday
Yes, I am sure because I used this way for another project.
– Niloufar Mohammadi
yesterday
Are you sure you have a bug in your test setup and not a bug in the system under test?
– John Wu
yesterday
Are you sure you have a bug in your test setup and not a bug in the system under test?
– John Wu
yesterday
Yes, I am sure because I used this way for another project.
– Niloufar Mohammadi
yesterday
Yes, I am sure because I used this way for another project.
– Niloufar Mohammadi
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
In unit tests you should not use connection to a DB. You should use mockups and create your own data to test with.
Use the NuGet package moq
to easily create mockup objects.
Example of using the mockup objects:
public void Test_Login()
{
Mock<IDatabase> mockDatabase = new Mock<IDatabase>();
mockDatabase.Setup(p => p.GetAccountAsync(It.IsAny<string>()))
.Returns((string givenEmail) => Task.FromResult(new Account(1, "test", givenEmail, "123", "$2b$10$pfsnDQ3IWuY/zER/uBQpedvRFntMNHGOGhOSpABKZ7bwS", false)));
Mock<IConfiguration> mockConfiguration = new Mock<IConfiguration>();
Mock<IHostingEnvironment> mockHostingEnvironment = new Mock<IHostingEnvironment>();
AccountService accountService = new AccountService(mockDatabase.Object, mockConfiguration.Object, mockHostingEnvironment.Object);
LoginViewModel loginViewModel = new LoginViewModel
{
EmailLogin = "test@test.com",
PasswordLogin = "s"
};
Task<Account> account = accountService.LoginAsync(loginViewModel);
Assert.NotNull(account.Result);
Assert.Equal(loginViewModel.EmailLogin, account.Result.Email);
}
In the example above I manually set the value of the mockup database that the service method will use to retrieve the account and compare the returned email with the given email.
Actually, I used mockups and now I want to test services connecting to the database. If you know how can I do it with multiple connections, please help me. Thank you!
– Niloufar Mohammadi
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
In unit tests you should not use connection to a DB. You should use mockups and create your own data to test with.
Use the NuGet package moq
to easily create mockup objects.
Example of using the mockup objects:
public void Test_Login()
{
Mock<IDatabase> mockDatabase = new Mock<IDatabase>();
mockDatabase.Setup(p => p.GetAccountAsync(It.IsAny<string>()))
.Returns((string givenEmail) => Task.FromResult(new Account(1, "test", givenEmail, "123", "$2b$10$pfsnDQ3IWuY/zER/uBQpedvRFntMNHGOGhOSpABKZ7bwS", false)));
Mock<IConfiguration> mockConfiguration = new Mock<IConfiguration>();
Mock<IHostingEnvironment> mockHostingEnvironment = new Mock<IHostingEnvironment>();
AccountService accountService = new AccountService(mockDatabase.Object, mockConfiguration.Object, mockHostingEnvironment.Object);
LoginViewModel loginViewModel = new LoginViewModel
{
EmailLogin = "test@test.com",
PasswordLogin = "s"
};
Task<Account> account = accountService.LoginAsync(loginViewModel);
Assert.NotNull(account.Result);
Assert.Equal(loginViewModel.EmailLogin, account.Result.Email);
}
In the example above I manually set the value of the mockup database that the service method will use to retrieve the account and compare the returned email with the given email.
Actually, I used mockups and now I want to test services connecting to the database. If you know how can I do it with multiple connections, please help me. Thank you!
– Niloufar Mohammadi
yesterday
add a comment |
up vote
1
down vote
In unit tests you should not use connection to a DB. You should use mockups and create your own data to test with.
Use the NuGet package moq
to easily create mockup objects.
Example of using the mockup objects:
public void Test_Login()
{
Mock<IDatabase> mockDatabase = new Mock<IDatabase>();
mockDatabase.Setup(p => p.GetAccountAsync(It.IsAny<string>()))
.Returns((string givenEmail) => Task.FromResult(new Account(1, "test", givenEmail, "123", "$2b$10$pfsnDQ3IWuY/zER/uBQpedvRFntMNHGOGhOSpABKZ7bwS", false)));
Mock<IConfiguration> mockConfiguration = new Mock<IConfiguration>();
Mock<IHostingEnvironment> mockHostingEnvironment = new Mock<IHostingEnvironment>();
AccountService accountService = new AccountService(mockDatabase.Object, mockConfiguration.Object, mockHostingEnvironment.Object);
LoginViewModel loginViewModel = new LoginViewModel
{
EmailLogin = "test@test.com",
PasswordLogin = "s"
};
Task<Account> account = accountService.LoginAsync(loginViewModel);
Assert.NotNull(account.Result);
Assert.Equal(loginViewModel.EmailLogin, account.Result.Email);
}
In the example above I manually set the value of the mockup database that the service method will use to retrieve the account and compare the returned email with the given email.
Actually, I used mockups and now I want to test services connecting to the database. If you know how can I do it with multiple connections, please help me. Thank you!
– Niloufar Mohammadi
yesterday
add a comment |
up vote
1
down vote
up vote
1
down vote
In unit tests you should not use connection to a DB. You should use mockups and create your own data to test with.
Use the NuGet package moq
to easily create mockup objects.
Example of using the mockup objects:
public void Test_Login()
{
Mock<IDatabase> mockDatabase = new Mock<IDatabase>();
mockDatabase.Setup(p => p.GetAccountAsync(It.IsAny<string>()))
.Returns((string givenEmail) => Task.FromResult(new Account(1, "test", givenEmail, "123", "$2b$10$pfsnDQ3IWuY/zER/uBQpedvRFntMNHGOGhOSpABKZ7bwS", false)));
Mock<IConfiguration> mockConfiguration = new Mock<IConfiguration>();
Mock<IHostingEnvironment> mockHostingEnvironment = new Mock<IHostingEnvironment>();
AccountService accountService = new AccountService(mockDatabase.Object, mockConfiguration.Object, mockHostingEnvironment.Object);
LoginViewModel loginViewModel = new LoginViewModel
{
EmailLogin = "test@test.com",
PasswordLogin = "s"
};
Task<Account> account = accountService.LoginAsync(loginViewModel);
Assert.NotNull(account.Result);
Assert.Equal(loginViewModel.EmailLogin, account.Result.Email);
}
In the example above I manually set the value of the mockup database that the service method will use to retrieve the account and compare the returned email with the given email.
In unit tests you should not use connection to a DB. You should use mockups and create your own data to test with.
Use the NuGet package moq
to easily create mockup objects.
Example of using the mockup objects:
public void Test_Login()
{
Mock<IDatabase> mockDatabase = new Mock<IDatabase>();
mockDatabase.Setup(p => p.GetAccountAsync(It.IsAny<string>()))
.Returns((string givenEmail) => Task.FromResult(new Account(1, "test", givenEmail, "123", "$2b$10$pfsnDQ3IWuY/zER/uBQpedvRFntMNHGOGhOSpABKZ7bwS", false)));
Mock<IConfiguration> mockConfiguration = new Mock<IConfiguration>();
Mock<IHostingEnvironment> mockHostingEnvironment = new Mock<IHostingEnvironment>();
AccountService accountService = new AccountService(mockDatabase.Object, mockConfiguration.Object, mockHostingEnvironment.Object);
LoginViewModel loginViewModel = new LoginViewModel
{
EmailLogin = "test@test.com",
PasswordLogin = "s"
};
Task<Account> account = accountService.LoginAsync(loginViewModel);
Assert.NotNull(account.Result);
Assert.Equal(loginViewModel.EmailLogin, account.Result.Email);
}
In the example above I manually set the value of the mockup database that the service method will use to retrieve the account and compare the returned email with the given email.
edited yesterday
answered yesterday
Svenmarim
6982527
6982527
Actually, I used mockups and now I want to test services connecting to the database. If you know how can I do it with multiple connections, please help me. Thank you!
– Niloufar Mohammadi
yesterday
add a comment |
Actually, I used mockups and now I want to test services connecting to the database. If you know how can I do it with multiple connections, please help me. Thank you!
– Niloufar Mohammadi
yesterday
Actually, I used mockups and now I want to test services connecting to the database. If you know how can I do it with multiple connections, please help me. Thank you!
– Niloufar Mohammadi
yesterday
Actually, I used mockups and now I want to test services connecting to the database. If you know how can I do it with multiple connections, please help me. Thank you!
– Niloufar Mohammadi
yesterday
add a comment |
Niloufar Mohammadi is a new contributor. Be nice, and check out our Code of Conduct.
Niloufar Mohammadi is a new contributor. Be nice, and check out our Code of Conduct.
Niloufar Mohammadi is a new contributor. Be nice, and check out our Code of Conduct.
Niloufar Mohammadi is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372648%2fmultiple-unit-tests-running-with-multiple-connections-to-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you sure you have a bug in your test setup and not a bug in the system under test?
– John Wu
yesterday
Yes, I am sure because I used this way for another project.
– Niloufar Mohammadi
yesterday