简体   繁体   中英

Mock a Property Getter with Rhino Mocks

Here is the property "LoggerName" of a class "ConfigurationSection".

/// <summary>
/// Gets The LoggerName.
/// </summary>
[ConfigurationProperty("LoggerName", IsRequired = true)]
public string LoggerName
{
    get { return Convert.ToString(this["LoggerName"]); }
}

If I create a stub or a strict mock of this type, when this property gets called, it throws an exception.

I've tried to mock this property with code such as:

Expect.Call(configSection.LoggerName).Return("LOREMIPSUM");

and

configSection.Stub(x => x.LoggerName).Return("LOREMIPSUM");

But this code calls that property getter and throws an exception.

I've looked in various places and all the methods I've tried do the same thing.

Does anyone know anything that might help me?

Many thanks in advance.

Ryan

For mocking classes with rhino, you'll have to mark the Property that you are mocking as virtual .

Can you try changing your property to this

public virtual string LoggerName
{
    get { return Convert.ToString(this["LoggerName"]); }
}

and see if you can get it to work?

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