views:

279

answers:

2

I am having a problem with a unit test I have created. First time doing this so I am not sure why I am getting this error

<DeploymentItem("ETDS.exe")> <DataSource("System.Data.SqlClient", "Data Source=Foo;Initial Catalog=FooDB;Integrated Security=True", "User_Names", DataAccessMethod.Sequential)> <TestMethod()> _
Public Sub ValidateUserNameTest()
    Dim target As Login_Accessor = New Login_Accessor ' TODO: Initialize to an appropriate value
    Dim expected As Boolean = True ' TODO: Initialize to an appropriate value
    Dim actual As Boolean
    actual = target.ValidateUserName
    Assert.AreEqual(expected, actual)
End Sub

The error I am getting is

Test method ETDS_Unit_Tests.LoginTest.ValidateUserNameTest threw exception: System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).

This is the same connection that my software is using, and the software runs fine, only the unit test is failing. What I am doing wrong?

Edit It Seems to fail one time for each datarow in the table. Also, I am using a Linq Query in the method that I being tested. I believe that this may be what is causing my error.

A: 

Because you are using integrated security, the problem is most likely: "Who is running the test and what rights do they have to the network and/or the SQL Server?". If it's a local machine account, it probably won't even be allowed out over the network.

I noticed that your connection string does not have Server=

If you are connecting locally, it should have Server=(local)

Cade Roux
This wouldnt be an issue. I am running this on the same box that I develop it on.
Aaron M
A: 

Do you have the Foo alias setup in your local SQL Native Client config? Without that the "Data Source = Foo;" reference in your connection string won't work.

BenA
Can you explain the SQL Native Client config? I am not sure if that alias is setup, but I suspect that is is an issue with the LINQ query
Aaron M
If you open SQL Server Configuration Manager (assuming your running either SQL 2005 or 2008), expand SQL Native Client Configuration in the tree pane, there is an Aliases node. If you don't have an entry called "Foo" in there, then your Data Source reference won't work, since it won't know where to look for Foo.
BenA
Nope, thats not the issue. Connection string works fine in the project, and is the same connection string that my application is using.
Aaron M