C# 如何填充接口的只读集合
我有一项任务必须完成,我需要通过下面的单元测试(我不能修改) 尽管没有太多内容,但JarofsweetCreator的代码如下:C# 如何填充接口的只读集合,c#,unit-testing,collections,C#,Unit Testing,Collections,我有一项任务必须完成,我需要通过下面的单元测试(我不能修改) 尽管没有太多内容,但JarofsweetCreator的代码如下: public class JarOfSweetsCreator : IJarOfSweetsCreator { public IJarOfSweets Create() { //throw new NotImplementedException(); } } 而IJarOfSweets具有以下特点: public inter
public class JarOfSweetsCreator : IJarOfSweetsCreator
{
public IJarOfSweets Create()
{
//throw new NotImplementedException();
}
}
而IJarOfSweets具有以下特点:
public interface IJarOfSweets : IReadOnlyCollection<ISweet>
{
void Shuffle ();
ISweet TakeSweetFromJar ();
}
公共接口IJarOfSweets:IReadOnlyCollection
{
voidshuffle();
ISweet TakeSweetFromJar();
}
我需要通过从IJarOfSweets
创建并计数30来通过测试。我不知道如何创建和计算一个接口的30个实例,如果你只能有一个,但我知道这听起来很愚蠢
我假设它与IJarOfSweets
接口的IReadOnlyCollection
部分有关,但我不知道如何使用它。IJarOfSweets
是像集合一样使用,还是在该界面中创建集合
如果它是一个只读集合,我应该如何制作30个呢?有时,最好的做法是编写尽可能少的代码,以便通过测试。这有助于您反思您的测试,并确保您正在编写有用的测试 目前,你的测试需要你做一些事情。它需要您实现
JarOfSweetsCreator
的Create
方法来返回实现IJarOfSweets
的内容,并且当您检查它的Count
属性时,它需要返回的对象返回30
第一步是创建一个实现接口的具体类JarOfSweets
。需要执行任何操作的方法是Count
属性,因此该类如下所示:
public class JarOfSweets : IJarOfSweets {
public void Shuffle()
{
throw new NotImplementedException();
}
public ISweet TakeSweetFromJar()
{
throw new NotImplementedException();
}
int IReadOnlyCollection<ISweet>.Count
{
get { return 30; }
}
IEnumerator<ISweet> IEnumerable<ISweet>.GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class JarOfSweetsCreator : IJarOfSweetsCreator
{
public IJarOfSweets Create()
{
var jar = new JarOfSweets();
for (int i = 0; i < 30; i++)
{
jar.Add(new Sweet());
}
return jar;
}
}
这是通过测试所需的最低要求。实际上,在编写更多测试/编写代码以使测试通过时,您可能希望对代码进行更多的处理
正如Jon在评论中所说,ReadOnlyCollection
,只是告诉您JarOfSweets
需要实现某些函数(get count+get enumerator)。这并不意味着收藏实际上需要是只读的。您可以使用任何内置集合类型来实现它,也可以编写自己的集合类型
重要的是要记住,当您知道实现接口的具体类型时,您还可以访问接口上未定义的方法。因此,例如,在JarOfSweetsCreator
中,它需要知道JarOfSweets
的实际实现类型,而不仅仅是接口才能实例化它。这意味着Create
方法可以调用基础集合上的其他方法,如Add
方法。因此,假设您有一个Sweet
类,该类实现了ISweet
,那么您可能会得到一个更像这样的创建者:
public class JarOfSweets : IJarOfSweets {
public void Shuffle()
{
throw new NotImplementedException();
}
public ISweet TakeSweetFromJar()
{
throw new NotImplementedException();
}
int IReadOnlyCollection<ISweet>.Count
{
get { return 30; }
}
IEnumerator<ISweet> IEnumerable<ISweet>.GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class JarOfSweetsCreator : IJarOfSweetsCreator
{
public IJarOfSweets Create()
{
var jar = new JarOfSweets();
for (int i = 0; i < 30; i++)
{
jar.Add(new Sweet());
}
return jar;
}
}
有时,最好的做法是编写尽可能少的代码,以使测试通过。这有助于您反思您的测试,并确保您正在编写有用的测试 目前,你的测试需要你做一些事情。它需要您实现
JarOfSweetsCreator
的Create
方法来返回实现IJarOfSweets
的内容,并且当您检查它的Count
属性时,它需要返回的对象返回30
第一步是创建一个实现接口的具体类JarOfSweets
。需要执行任何操作的方法是Count
属性,因此该类如下所示:
public class JarOfSweets : IJarOfSweets {
public void Shuffle()
{
throw new NotImplementedException();
}
public ISweet TakeSweetFromJar()
{
throw new NotImplementedException();
}
int IReadOnlyCollection<ISweet>.Count
{
get { return 30; }
}
IEnumerator<ISweet> IEnumerable<ISweet>.GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class JarOfSweetsCreator : IJarOfSweetsCreator
{
public IJarOfSweets Create()
{
var jar = new JarOfSweets();
for (int i = 0; i < 30; i++)
{
jar.Add(new Sweet());
}
return jar;
}
}
这是通过测试所需的最低要求。实际上,在编写更多测试/编写代码以使测试通过时,您可能希望对代码进行更多的处理
正如Jon在评论中所说,ReadOnlyCollection
,只是告诉您JarOfSweets
需要实现某些函数(get count+get enumerator)。这并不意味着收藏实际上需要是只读的。您可以使用任何内置集合类型来实现它,也可以编写自己的集合类型
重要的是要记住,当您知道实现接口的具体类型时,您还可以访问接口上未定义的方法。因此,例如,在JarOfSweetsCreator
中,它需要知道JarOfSweets
的实际实现类型,而不仅仅是接口才能实例化它。这意味着Create
方法可以调用基础集合上的其他方法,如Add
方法。因此,假设您有一个Sweet
类,该类实现了ISweet
,那么您可能会得到一个更像这样的创建者:
public class JarOfSweets : IJarOfSweets {
public void Shuffle()
{
throw new NotImplementedException();
}
public ISweet TakeSweetFromJar()
{
throw new NotImplementedException();
}
int IReadOnlyCollection<ISweet>.Count
{
get { return 30; }
}
IEnumerator<ISweet> IEnumerable<ISweet>.GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class JarOfSweetsCreator : IJarOfSweetsCreator
{
public IJarOfSweets Create()
{
var jar = new JarOfSweets();
for (int i = 0; i < 30; i++)
{
jar.Add(new Sweet());
}
return jar;
}
}
“IJarOfSweets是否像集合一样使用”-是的,这就是为什么它实现了
IReadOnlyCollection
。。。它是用来收集糖果的,尽管用Shuffle
方法也很奇怪。老实说,如果您在这方面遇到问题,最好向您的老师询问更多详细信息,因为这表明您可能遗漏了一些概念。好的,但是如果集合是只读的,我将如何创建30?IReadOnlyCollection是一个坏名字-接口只让成员读取集合,而不是对其进行修改。“IJarOfSweets是否像一个集合一样使用“-是的,这就是它实现IReadOnlyCollection
的原因。”。。。它是用来收集糖果的,尽管用Shuffle
方法也很奇怪。老实说,如果您在这方面遇到问题,最好向您的老师询问更多细节,因为这表明您可能遗漏了一些概念。好的,但是如果集合是只读的,我将如何创建30?IReadOnlyCollection是