chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I keep getting an exception in my target when trying to call into the collection to retrieve an item.

what might i be doing wrong?

here's the exception:

Microsoft.TeamFoundation.WorkItemTracking.Client.FieldCollection.GetById(Int32 id)
UpdateAssignee.Execute() in C:devTrimble.MobileSolutionscurrentuildTasksTeamBuildUpdateAssignee.cs: line 16
TeamBuild.Test.UpdateAssigneeTest.CallExecute() in C:devTrimble.MobileSolutionscurrentuildTasksTeamBuild.TestUpdateAssigneeTest.cs: line 49


here's the setup code:

UpdateAssignee target = this.GetUpdateAssignee();
UpdateAssignee_Accessor accessor = UpdateAssignee_Accessor.AttachShadow(target);

TeamFoundationServer mockedTfs = RecorderManager.CreateMockedObject<TeamFoundationServer>(Constructor.Mocked, "http://nowhere.com");
WorkItemStore mockedWis = RecorderManager.CreateMockedObject<WorkItemStore>(Constructor.Mocked, mockedTfs);
WorkItemType wit = RecorderManager.CreateMockedObject<WorkItemType>(Constructor.Mocked);
WorkItem mockedWi = RecorderManager.CreateMockedObject<WorkItem>(Constructor.Mocked, wit );
FieldCollection mockedFields = RecorderManager.CreateMockedObject<FieldCollection>(Constructor.Mocked );
Field mockedField = RecorderManager.CreateMockedObject<Field>(Constructor.Mocked);

// Start Mocking
using (RecordExpectations recorder = new RecordExpectations())
{
recorder.ExpectAndReturn(mockedTfs.GetService(typeof(WorkItemStore)), mockedWis).RepeatAlways();
recorder.ExpectAndReturn(mockedWis.GetWorkItem(target.WorkItemId), mockedWi).RepeatAlways();
recorder.ExpectAndReturn(mockedWi.Fields, mockedFields).RepeatAlways();
recorder.ExpectAndReturn(mockedFields.GetById((int)CoreField.AssignedTo), mockedField).RepeatAlways();
mockedField.Value = "Tim Bassett";

}
asked by tbassett22030 (600 points)

2 Answers

0 votes
Hi Tim,
Can you post the definition of the "Fields" member defined in "WorkItem"?

The problem is most likely one of the following two:
1) Fields is a public field (not a property) which does not get initialized since the constructor for mockedWi is mocked.
(Currently TypeMock is not capable to mocking fields)
2) Fields is a concrete collection class defined in mscorlib. TypeMock can only mock interfaces from mscrolib (and not real classes).
If this is the case, you can replace the Fields to be an interface and then you will be able to mock it.
answered by lior (13.2k points)
0 votes
Lior,

Fields is FieldCollection from Microsoft.TeamFoundation.WorkItemTracking.Client

namespace Microsoft.TeamFoundation.WorkItemTracking.Client
{
public sealed class FieldCollection : ReadOnlyList
{
public override int Count { get; }

public Field this[CoreField coreField] { get; }
public Field this[int index] { get; }
public Field this[string name] { get; }

public bool Contains(Field value);
public bool Contains(int id);
public bool Contains(string fieldName);
public Field GetById(int id);
protected override object GetItem(int index);
public int IndexOf(Field value);
}
answered by tbassett (1.8k points)
...