Invalid Setup on a non-overridable member when moqing a property of LinqToSql class.

7,106 views
Skip to first unread message

rroman81

unread,
Jun 14, 2009, 10:33:30 PM6/14/09
to Moq Discussions
I am moqing a few objects that were generated by linq to sql designer.
EventReminder has a 1-to-1 to Appoint. The generator created a
property EventReminder.Appoint. It looks pretty straight forward.
However, this fails:

var stubEvt = new Mock<EventReminder>();
var mockAppt = new Mock<Appointment>();

stubEvt.setupProperty(e=>e.Appointment, mockAppt.Object); < ----
error here

The declaration for EventReminder in DataModel.designer.cs
public class EventReminder : IPropertyChanging, IPropertyChanged
{
private Appointment _Appointment;

public Appointment Appointment { get { return _Appointment; } set
{....}}
}

Is this due to the fact that properties/methods are non-virtual in C#
by default?

Daniel Cazzulino

unread,
Jun 20, 2009, 2:17:42 AM6/20/09
to moq...@googlegroups.com
yes.
moq does for you what you'd have had to do yourself: inherit the class and override everything. if something is not virtual, it's not "hook-able".

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471

rroman81

unread,
Jun 20, 2009, 6:20:43 PM6/20/09
to Moq Discussions
Thanks for the reply. It's unfortunate that Linq To Sql generator
makes the code non-test friendly. I am forced to change property/
methods to virtual in the *.designer.cs file and redo that every time
it gets regenerated. Also, for those that are embarking on that
journey and decide to use the partial class to add additional
functionality, there is very nasty issue that I had to deal with,
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361577.

Thanks,

Roman

On Jun 19, 11:17 pm, Daniel Cazzulino <kzu....@gmail.com> wrote:
> yes.
> moq does for you what you'd have had to do yourself: inherit the class and
> override everything. if something is not virtual, it's not "hook-able".
>
> /kzu
>
> --
> Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> 425.329.3471
>

Brad Wilson

unread,
Jun 21, 2009, 11:38:01 AM6/21/09
to moq...@googlegroups.com
LINQ to SQL can use T4 templates for code generation, which means you could use a template that generates the code the way you want it the first time rather than constantly changing it.

More infomration of L2S and T4: http://l2st4.codeplex.com/

--
Brad    http://bradwilson.typepad.com/
Reply all
Reply to author
Forward
0 new messages