Can Rhino do this kind of stub as Moq?

31 views
Skip to first unread message

FrankM

unread,
Dec 22, 2008, 6:27:57 PM12/22/08
to Rhino.Mocks
My Moq code:
===================
var mock = new Mock();
// start “tracking” sets/gets to this property
mock.Stub(v => v.Value);
// provide a default value for the stub’ed property
mock.Stub(v => v.Value, 5);

Now I can do:

IHaveValue v = mock.Object;
// Initial value was stored
Assert.Equal(5, v.Value);

// New value set which changes the initial value
v.Value = 6;
Assert.Equal(6, v.Value);
=====================

How can I do the similar stub using Rhino? Thanks.

Ayende Rahien

unread,
Dec 22, 2008, 7:44:58 PM12/22/08
to Rhino...@googlegroups.com
var mock = MockRepository.CreateStub<IFoo>();

mock.Value = 5;

Assert.Equal(5, mock.Value);

mock.Value = 6;

Assert.Equal(6, mock.Value);

FrankM

unread,
Dec 23, 2008, 10:35:54 AM12/23/08
to Rhino.Mocks
Thanks. But my MockRepository doesn't have a method called CreateStub,
neither in static nor in instance. My version is 3.5.0.2.

Am I missing something basic?

On Dec 22, 5:44 pm, "Ayende Rahien" <aye...@ayende.com> wrote:
> var mock = MockRepository.CreateStub<IFoo>();
>
> mock.Value = 5;
>
> Assert.Equal(5, mock.Value);
>
> mock.Value = 6;
>
> Assert.Equal(6, mock.Value);
>

FrankM

unread,
Dec 23, 2008, 10:39:29 AM12/23/08
to Rhino.Mocks
Sorry, it's GenerateStub(). It works now.

Thanks for your help, very much.

On Dec 22, 5:44 pm, "Ayende Rahien" <aye...@ayende.com> wrote:
> var mock = MockRepository.CreateStub<IFoo>();
>
> mock.Value = 5;
>
> Assert.Equal(5, mock.Value);
>
> mock.Value = 6;
>
> Assert.Equal(6, mock.Value);
>

FrankM

unread,
Dec 23, 2008, 2:57:24 PM12/23/08
to Rhino.Mocks
Something wrong in mock.Raise() on this kind of stub. It seems this
stub behavior is not available inside the event handler?

My test like this:
[Test]
public void should_insert_a_new_item_before_the_current_row
()
{
// Arrange , _mockView = MockRepository.GenerateStub()
_mockView.IndexOfCurrentRow = 1;

// Action
_presenter.InitView();

// This still works
Assert.That(_mockView.IndexOfCurrentRow, Is.EqualTo
(1));

_mockView.Raise(x => x.InsertItem += null, this,
EventArgs.Empty);

// Assert
Assert.That(_mockView.List.Count, Is.EqualTo
(_stubList.Count + 1));

// This will fail, the property stub doesn't work in
view.Raise()??
Assert.That(_mockView.IndexOfCurrentRow, Is.EqualTo
(1));

}

I have an event in presenter do the insertItem,

protected virtual void InsertItem(object sender, EventArgs e)
{
var list = _view.List;

int rowIndex = _view.IndexOfCurrentRow; // This will
always be zero, no matter what I set in test. Unless I change
_mockview from GenerateStub to GeneratMock(), but then I can't stub
view.List property anymore.

var newBO = ...;

list.Insert(rowIndex, newBO);

_view.List = list;

_view.IndexOfCurrentRow = rowIndex;
}



On Dec 22, 5:44 pm, "Ayende Rahien" <aye...@ayende.com> wrote:
> var mock = MockRepository.CreateStub<IFoo>();
>
> mock.Value = 5;
>
> Assert.Equal(5, mock.Value);
>
> mock.Value = 6;
>
> Assert.Equal(6, mock.Value);
>

Cvetomir Todorov

unread,
Dec 26, 2008, 3:46:22 PM12/26/08
to Rhino...@googlegroups.com
If you are able to create an isolated test with the issue (meaning it doesn't contain anything connected with your application) it will be much easier for others to see what's wrong.
--
Software modules coupling is the path to the dark side.
Coupling leads to complexity.
Complexity leads to confusion.
Confusion leads to suffering.
Once you start down the dark path, forever will it dominate your destiny, consume you it will!

FrankM

unread,
Jan 1, 2009, 7:15:39 PM1/1/09
to Rhino.Mocks
OK, here is an isolated one.

var mockvVew = MockRepository.GenerateStub();
mockView.Something = 1;
Assert.Equals(1, mockView.Something);

mockView.Raise(x => x.SomeEvent += null, this, EventArgs.Empty);

//try to read mockView.Something in event handler got zero, instead of
1.


On Dec 26 2008, 1:46 pm, "Cvetomir Todorov"
Reply all
Reply to author
Forward
0 new messages