简体   繁体   中英

Using Rhino Mocks how to check the value of a Struct Field in the parameter passed to a mock object?

In Rhino Mocks, I am testing for a method called Store in the Subject class. The Subject.Store(Member) internally calls IStore.Store(Person). How do I check that the name parameter that i set on the Member is the same name that is present in the Person parameter in the call to IStore.Store(Person).

Obviously, having an Equals method implemented in the struct and then calling Arg.Is.Equals would be an option. But I do not have control over the source code for Person or Member structs.

Here is the code snippet..

struct Person {
        string name;
        int age;
        char sex;
    }

    struct Member {
        string name;
        string address;
        string departnemt;

        public Member(string name, string address, string departnemt) {
            // TODO: Complete member initialization
            this.name = name;
            this.address = address;
            this.departnemt = departnemt;
        }
        //other methods
    }

    interface IStore {
        void Store(Person p);
        //other methods
    }

    class Subject {
        IStore db;
        public void Store(Member m) {
            //some logic to convert Member to Person
            Person p = GetPersonFromMember(m);
            db.Store(p);
        }

        //other methods
    }

[Test]
        public void TestStore() {
            //Arrange
            Member m = new Member("dave", "crawford Ave", "Physics");

            var mockStore = MockRepository.GenerateMock<IStore>();
            mockStore.Expect(x => x.Store(Arg<Person>.Is.NotNull));
            //here i also want to check that the Person.Name is "dave"
            //how can i do this?

            //Act
            subject.Commit();

            //Assert
            mockStore.VerifyAllExpectation();
        }

Instead of or in addition to doing Arg<Person>.Is.NotNull you can do Arg<Person>.Property.Value("Name", m.Name)

Also assuming you are doing .Net 3.5 or above you can do something which you may or may not think reads better and avoids the string with the property name: Arg<Person>.Matches(p => p != null && p.Name == m.Name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM