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.










share|improve this question







New contributor




Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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.










share|improve this question







New contributor




Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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.










share|improve this question







New contributor




Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Niloufar Mohammadi

61




61




New contributor




Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Niloufar Mohammadi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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










  • 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












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 moqto 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.






share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Niloufar Mohammadi is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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

























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 moqto 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.






share|improve this answer























  • 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















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 moqto 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.






share|improve this answer























  • 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













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 moqto 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.






share|improve this answer














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 moqto 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.







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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










Niloufar Mohammadi is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]