• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 52
  • Last Modified:

Trying to learn about Unit Tests

I'm following the example on https://docs.microsoft.com/en-gb/visualstudio/test/walkthrough-creating-and-running-unit-tests-for-managed-code
to learn about unit testing.
When I add the reference to Bank.sln I'm getting a message:

A reference to D:\Documents\UnitTesting\Bank\Bank.sln could not be added.  Please make sure that the file is accessible, and that it is a valid assembly or COM component.
Could it be because Visual Studio 2017 is installed on my C drive and this project is on my D drive?  I'm using C#.net
0
AlHal2
Asked:
AlHal2
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Chinmay PatelEnterprise ArchitectCommented:
Can you explain where it is asking to add a reference to Bank.sln, generally you add reference to assemblies, not a solution file.
0
 
AlHal2Author Commented:
Here are the instruction I've followed.  I've also attached a screenshot.

Create a project to test
Open Visual Studio.

On the File menu, select New > Project.

The New Project dialog box appears.

Under Installed Templates, click Visual C#.

In the list of application types, click Class Library.

In the Name box, type Bank and then click OK.

Note

If the name "Bank" is already used, choose another name for the project.

The new Bank project is created and displayed in Solution Explorer with the Class1.cs file open in the code editor.

Note

If the Class1.cs file is not open in the Code Editor, double-click the file Class1.cs in Solution Explorer to open it.

Copy the source code from the Sample Project for Creating Unit Tests, and replace the original contents of Class1.cs with the copied code.

Save the file as BankAccount.cs.

On the Build menu, click Build Solution.

You now have a project named Bank. It contains source code to test and tools to test it with. The namespace for Bank, BankAccountNS, contains the public class BankAccount, whose methods you'll test in the following procedures.

In this article, the tests focus on the Debit method. The Debit method is called when money is withdrawn from an account. Here is the method definition:

C#

Copy
// Method to be tested.
public void Debit(double amount)
{
    if(amount > m_balance)
    {
        throw new ArgumentOutOfRangeException("amount");
    }
    if (amount < 0)
    {
        throw new ArgumentOutOfRangeException("amount");
    }
    m_balance += amount;
}
Create a unit test project
On the File menu, select Add > New Project.

In the New Project dialog box, expand Installed, expand Visual C#, and then choose Test.

From the list of templates, select Unit Test Project.

In the Name box, enter BankTests, and then select OK.

The BankTests project is added to the Bank solution.

In the BankTests project, add a reference to the Bank project.

In Solution Explorer, select References in the BankTests project and then choose Add Reference from the context menu.

In the Reference Manager dialog box, expand Solution and then check the Bank item.
Screenshot.docx
0
 
ste5anSenior DeveloperCommented:
Just a comment:

You set references in solutions to projects.. maybe that is the problem..

but really, an image embedded in a Word instead of attaching it directly or embedding it into your post using the image button in the toolbar?

Capture.PNG
2
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Chinmay PatelEnterprise ArchitectCommented:
As mentioned earlier, you are supposed to add reference to the assembly of Banks project.
References Section
0
 
ste5anSenior DeveloperCommented:
E.g. setting the reference directly to the project:

Capture.PNG
0
 
käµfm³d 👽Commented:
Could it be because Visual Studio 2017 is installed on my C drive and this project is on my D drive?
Nope, that has nothing to do with anything. You can even add a reference from a network share if your account can see the share. The problem is as other mentioned.
0
 
AlHal2Author Commented:
This is what I did.

In Solution Explorer, select References in the BankTests project and then choose Add Reference from the context menu.

I selected Banks.sln by checking the box then clicking OK.  Then I got the error message above.

Is Banks.sln the right file to look for?
0
 
ste5anSenior DeveloperCommented:
No, it's not. As I already wrote, you can only reference projects.

Look at the image I've posted:

You need to select Projects>Solutions on the left side. Then you should see all projects of your current solution minus the actual one, where you want to add the reference.

When you still cannot find it, maybe it's time to consider posting more information, like an image of your current solution?!
0
 
AlHal2Author Commented:
Sorry.  I thought an sln file represented a project.
I then hit on the idea of building the solution and linking this to the DLL file.
That seemed to work.  Am I correct now?
0
 
ste5anSenior DeveloperCommented:
Maybe. Referencing the assembly directly, means that you can test against an old build. Referencing against the project ensures that you always uses at least the latest build.

sln is solution. Just strip the vowels and the t.
0
 
AlHal2Author Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now