0

Possible Duplicate:
What are the differences between mocks and stubs on Rhino Mocks?

I am using mocks of unit testing..But I can't get the difference between mock and stub in the implementation code.. the mock implementation code is:-

[TestFixture]
    public class MockUser
    {
        [Test]
        public void SaveValidUserFileNameUsingMock()
        {
            UserMock um = new UserMock();
            um.uName = "";
            um.fName = "sfs.jpg";
            um.ContentType = "image/jpg";
            IUser usr = um;
            Assert.AreEqual("E:/image/kawade.jpg", usr.Save(um));
        }
    }

    public class UserMock : IUser
    {
        public string uName;
        public string fName;
        public string ContentType;

        public string Save(IUser u)
        {
            if (uName == "" || fName == "")
            {
                throw new ArgumentException("missing field name");
            }

            if (ContentType.Contains("image"))
            {
                string ext = Path.GetExtension(fName);
                return (string.Format("E:/image/{0}", this.uName + ext));
            }
            return "invalid";
        }
    }


        public interface IUser
        {
            string Save(IUser u);
        }

and the class to test is:-

public class User
    {
        public string uName;
        public string fName;
        //private IUser usr;

        public void Save(FileUpload fu, User usr)
        {
            if (uName == null || fName == null)
            {
                throw new ArgumentException("missing field name");
            }

            if (fu.PostedFile.ContentType.Contains("image"))
            {
                string ext = Path.GetExtension(fName);
                fu.SaveAs(string.Format("E:/image{0}", this.fName + ext));
            }
        }

and the same class is being used for stub testing. the stub testing code is:-

[TestFixture]
    public class UserTest
    {
        [Test]
        public void SaveUserValidFile()
        {
            UserStub su = new UserStub();
            su.uName = "kawade";
            su.fileName = "sfgs.png";
            su.Contenttype = "image/x-png";
            su.pName = "sdskjh";
            IUser target = su;

        }
    }

    internal class UserStub : IUser
    {
        public string uName;
        public string pName;
        public string Contenttype;
        public string fileName;


        public string Save(IUser u)
        {
            if (uName == null || pName == null)
            {
                throw new ArgumentException("user_name or pic_name is required");
            }

            if (Contenttype.Contains("image"))
            {
                string ext = Path.GetExtension(fileName);
                //return string.Format("C:/test/{0}", this.uName + ext);
                Assert.AreEqual("C:/Test/kawade.png", target.Save(su));
            }
            return "";
        }
    }

    public  interface IUser
    {
        string Save(IUser u);
    }

please someone let me know the difference between stub and mock and it's implementation in above code..

Community
  • 1
  • 1
user132
  • 39
  • 4
  • 2
    @Mitch Wheat: It's not relevant if you Google for him or not. (almost) ***Everything*** can be found if googling long enough. Either answer the question or close it as a duplicate. We do want questions here and to be the #1 site for programming question, don't we? – jgauffin Apr 26 '11 at 08:09
  • @ jgauffin : it took me 5 seconds. alot less time than the poster required to create his question. And BTW I have voted to close as a dupe. I suggest you do the same. – Mitch Wheat Apr 26 '11 at 08:17
  • 2
    @Mitch Wheat: imho it doesn't matter if you found it in 5 seconds. All programming questions should be welcome, even it they are easy to find in google. And I've now voted to close the question. – jgauffin Apr 26 '11 at 08:21
  • @jgauffin : I disagree. If you can find an existing answer in 5 seconds, just go find it. – Mitch Wheat Apr 26 '11 at 12:10
  • @Mitch Wheat: Read at meta: http://meta.stackexchange.com/questions/88818/are-all-questions-welcome – jgauffin Apr 26 '11 at 12:14
  • Closing as duplicate isn't a great idea. He's gone to the trouble of posing his code example and does not mention rhino mocks which the possible duplicate is using explicitly. – Adam Dymitruk Apr 26 '11 at 14:22
  • @jgauffin : whatever it says at meta and whatever the owners of SO want, SO will never be google! – Mitch Wheat Apr 26 '11 at 14:28

1 Answers1

1

Stub: something that gives you a stock answer or, at most, property bag behavior or methods that do nothing but must return void.

Fake: something that gives you staged behavior so you can artificially test a few scenarios of a client of that class.

Mock: something that can be programmed to give different responses at different times that it's called. It will give you control in terms of if you want the calls to be checked for what order they were called in. most mock frameworks give you stub functionality as well.

Partial variation of each of the above: via a fake, one can wrap a stub, another fake, a mock or the real implementation and do something for specific arguments only.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141