Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a C# unit test in Visual Studio?

This is my first unit test and wanted some help clearing out my thoughts about the process of writing a unit test.

I wanted to write a test method that will add a new user - using my AddUser method in my library class.

Document doc = new Document();

[TestMethod]
public string AddUser()
{
    string name = doc.AddUser("Testing User");
    Assert.IsNotNull(name);
}

The error I am getting on build:

Cannot implicitly convert type void to string

This is my AddUser method:

public void AddUser(string newUserName)
{
    using (var db = new DataContext())
    {
        User user = new User()
        {
            FullName = newUserName,
            ID = Guid.NewGuid()
        };

        db.Users.InsertOnSubmit(user);
        db.SubmitChanges();
    }
}
like image 689
Masriyah Avatar asked Dec 09 '22 17:12

Masriyah


1 Answers

Your method does not have a return value:

public void AddUser
       ^^^^ no return value

So you can't store it into a string:

string name = doc.AddUser("Testing User");
^^^^^^^^^^^ AddUser has no return value
like image 99
mellamokb Avatar answered Dec 11 '22 07:12

mellamokb