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

asp.net-mvc, c#, unit-testing

Solution

Make sure you return the name from from your `AddUser(string newUserName)` method.

Replace your method like

public String AddUser(string newUserName)
{
    using (var db = new DataContext())
    {
        User user = new User()
        {
            FullName = newUserName,
            ID = Guid.NewGuid()
        };
        db.Users.InsertOnSubmit(user);
        db.SubmitChanges();
    }
    return newUserName;
}

Problem

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(); } } ```

Original source