8 Replies - 641 Views - Last Post: 06 June 2017 - 01:12 PM Rate Topic: -----

#1 San94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 17

Testing a Function

Posted 06 June 2017 - 10:58 AM

public static void CreateUser(int userId,string name, System.DateTime createdAt,string createdBy, System.DateTime modifiedAt,
            string modifiedBy, string address,string city,string province, string country,string postcode, string phone)
        {
            try
            {
                using (UserDbContext _usermain = new UserDbContext())
                {
                    User newuser = new User()
                    {
                        UserId = userId,
                        Name = name,
                        CreatedAt = createdAt,
                        CreatedBy = createdBy,
                        ModifiedBy = modifiedBy,
                        ModifiedAt = modifiedAt,
                        Address = address,
                        City = city
                    };
                    _usermain.Users.Add(newuser);
                    _usermain.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                string error = string.Format("Error in Creating User", ex.Message);
                throw new Exception(error, ex);
            }
        

    }


:code:
CAN anyone please help me to write a unit test for this function

This post has been edited by modi123_1: 06 June 2017 - 11:18 AM
Reason for edit:: In the future, use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: Testing a Function

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,847
  • Joined: 12-June 08

Re: Testing a Function

Posted 06 June 2017 - 11:18 AM

What have you tried, thought about, or considered?
Was This Post Helpful? 0
  • +
  • -

#3 San94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 17

Re: Testing a Function

Posted 06 June 2017 - 11:20 AM

View Postmodi123_1, on 06 June 2017 - 11:18 AM, said:

What have you tried, thought about, or considered?



 [TestMethod]
        public void CreateUser_Test()
        {
            UserRepository.DeleteAllUser();
            List<User> user = UserRepository.GetAllUsers();
            //Assert.IsNotNull(reportuser);
            Assert.AreEqual(0, user.Count);
            var item = UserRepository.GetAllUsers();
            foreach (var i in item)
            {
                UserRepository.CreateUser(i.UserId, "Ali", DateTime.Now, "Saniya", DateTime.Now, "Saniya", "Pak",
                    "Rwp", "Punjab", "Pakistan", "46000", "03361054");
            }
            var allUser = UserRepository.GetAllUsers();
            Assert.AreEqual(allUser.Count(), item.Count());
        }

:code:
But I have no idea it is correct or not

This post has been edited by modi123_1: 06 June 2017 - 11:22 AM
Reason for edit:: In the future, use the [code] tag button in the editor.

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,847
  • Joined: 12-June 08

Re: Testing a Function

Posted 06 June 2017 - 11:23 AM

Well... what are the results?

PS - try and use the [code ] tag button in the editor.
Was This Post Helpful? 0
  • +
  • -

#5 San94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 17

Re: Testing a Function

Posted 06 June 2017 - 11:30 AM

View Postmodi123_1, on 06 June 2017 - 11:23 AM, said:

Well... what are the results?

PS - try and use the [code ] tag button in the editor.


The Test Failed and I am getting an exception System.Data.Core.MetaDataExceptions: Unable to load the specified metadata resource
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,847
  • Joined: 12-June 08

Re: Testing a Function

Posted 06 June 2017 - 11:53 AM

Mkay.. what line is that happening on?
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: Testing a Function

Posted 06 June 2017 - 11:58 AM

That test is a bit of a muddle. A more sensible first test, in my opinion, would be count how many users there are, add a user, then see if the count has increased by one.

(Of course, if the users are persisted, then you'd want to ensure that you create a distinct, new, user with each test run. I suppose you could eventually pair-up this test with another one that removes the user.)



In any case, being new to testing, you should start with extremely simple ones. A wide range of simple, focused, tests is the way to pursue this subject.
Was This Post Helpful? 0
  • +
  • -

#8 San94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 17

Re: Testing a Function

Posted 06 June 2017 - 01:05 PM

Does that means I haven't written the correct test?

This post has been edited by andrewsw: 06 June 2017 - 01:13 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: Testing a Function

Posted 06 June 2017 - 01:12 PM

I find it hard to follow. You call GetAllUsers three times, reuse the identifier 'item' and name a list of users 'user'. You also make two Asserts, none of which are a great idea with your first attempt. As I've said, go simple.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1