0

I have a solution with an MVC project containing a Services project on top of a Core project.

I just added a Unit Tests project and referenced Core and Services - I'm trying to test Services.

I have a basic call in the test:

public class CrudTests
    {
        private readonly SetServices _setService = new SetServices();

        [TestMethod]
        public void TestMethod()
        {
            _setService.CreateSet("Test Set", "Test Set Details", null);

Which ends up failing because the test can't connect to a database. My config has this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\;Initial Catalog=Project.Services.Tests;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Project.Services.Tests.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

I've tried by creating the database Project.Services.Tests and then running, but I get this:

Message=Database 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\App.Services.Tests.mdf' already exists. Choose a different database name. Cannot attach the file 'C:\PROJECTS\App\App\Services.Tests\bin\Debug\Services.Tests.mdf' as database 'App.Services.Tests'. Source=.Net SqlClient Data Provider ErrorCode=-2146232060 Class=16 LineNumber=65536 Number=1801 Procedure="" Server=.\ State=2 StackTrace:

I tried deleting the database and letting the test do it's thing, and I get this:

A file activation error occurred. The physical file name '\Project.Services.Tests.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation.

How can I get this working properly?

2
  • 1
    IMO Unit Test means Without DB interaction, You can use Fake or Mock to do Unit testing. If You Unit test with DB, then Your will test will be slow like tortoise :D Commented Feb 21, 2013 at 4:33
  • I get that - I'm just trying to learn one thing at a time. Commented Feb 21, 2013 at 15:11

2 Answers 2

2

In Unit Testing you must test the code without any dependency such as database or file system etc. unit test means testing only that part of code independently. if you want to test your code by its integration to database you must create Integration Test. that means you should create an initializing for database usage before running test and a tear down for diposing resources after finishing tests.

maybe this link help you more to understand the difference between Unit Test and Integration Test

Sign up to request clarification or add additional context in comments.

3 Comments

I'm just trying to learn one thing at a time here... unit testing with the database seems simpler - once I get that, I'll pull in mocks.
the simplicity is not matter in testing! you must do the right type of testing for each porpuse. but as I said, you can create initialize and tear down methods for runnig tests against database.
I see this answer a lot and I find it irritating - not because its wrong - but because the DAL is a layer in your application and should separate from the actual database implementation (and therefore unit-testable). One suggestion I have seen is to use an in-memory database (e.g. LocalDB or MDF) mocking the production schema but with a relatively low footprint - blogs.interknowlogy.com/2014/08/29/unit-testing-using-localdb
0

you should just fix the connection string and it should work, You are attaching the database file from |DataDirectory| of the test project,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.