简体   繁体   中英

Mocking complex objects like selenium webdriver with Moq

I'm trying to Mock the selenium web driver. I use the following code:

    public  IWebDriver GetMockDriver()
    {
        this.Mock = new Mock<IWebDriver>();
        return (IWebDriver)this.Mock.Object;            
    }

If I'm just accessing driver properties, everything is fine, but when I try to access an object that is part of Driver, eg:

             driver.Manage().Window.Maximize();

I get back a System.NullReferenceException.

I don't really want/need to track calls that were made to the Mock object, it's more that I want something that implements iWebDriver so I can write my selenium tests using TDD and not having to actually create an instance of a real web driver each time.

You have to define an implementation on your mock if you want to call a specific member

this.Mock.Setup(x => x.SomeMethod()).Returns(someObject);

Try some variation on the above. Otherwise your mocked methods will return the defualt data type for the member (probably null)

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