我在单元测试一个方法时遇到了麻烦,该方法改变了传递给它的引用类型的一些属性.
举个例子,假设我有一个名为Policy的类.
Policy policy = new Policy();
policy.Status = Active;
Run Code Online (Sandbox Code Playgroud)
然后,我将此策略传递给策略管理器,以便取消激活策略.
policyManager.InactivatePolicy(policy);
Run Code Online (Sandbox Code Playgroud)
inactivate策略方法执行以下操作:
public void InactivatePolicy(Policy policy)
{
policy.Status = Inactive;
UpdatePolicy(policy); //saves the updated policy details via nhibernate
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是单元测试这个DoSomething方法.(忽略它在这个例子中的作用是无用的这一事实)
public void DoSomething(Policy policy)
{
Policy policy = new Policy();
policy.Status = Active;
policyManager.InactivatePolicy(policy);
}
Run Code Online (Sandbox Code Playgroud)
因为我模拟了策略管理器,所以状态不会被设置为非活动状态,因此当我断言在调用DoSomething之后策略的状态处于非活动状态时,由于它仍处于活动状态,因此测试失败.
[Test]
public void TheStatusShouldBeInactiveWhenWeDoSomething()
{
Mock<IPolicyManager> policyManagerMock = new Mock<PolicyManager>();
MyClass mc = new MyClass(policyManagerMock.Object);
Policy policy = new Policy();
policy.Status = Active;
mc.DoSomething(policy);
Assert.That(policy.Status, Is.EqualTo(Inactive)); //this fails
}
Run Code Online (Sandbox Code Playgroud)
所以我处在这样的情况下,代码在现实中工作,但在我的单元测试中被隔离.
我能够解决这个问题的唯一方法是让策略管理器的InactivatePolicy方法返回修改后的策略,以便我可以模拟预期的返回值.
public Policy …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
List<Person> people = new List<Person>
{
new Person{ Id = 1, Name = "Bob"},
new Person{ Id = 2, Name = "Joe"},
new Person{ Id = 3, Name = "Bob"}
};
var peopleGroupedByName = from p in people
group p by p.Name;
//get all groups where the number of people in the group is > 1
Run Code Online (Sandbox Code Playgroud)
对于我的生活,我无法弄清楚如何使用linq查询返回的值,以便能够过滤所有返回的组,以便我只有包含多个项目的组.
目前,我正在撞墙,我无法想到在Google搜索中使用哪些关键字以便自己解决问题.
我非常感谢Linq中如何做到这一点的任何帮助,因为它看起来应该很简单.
我在C#中有以下两个类:
public class MyFirstClass : IMyFirstClass
{
MySecondClass mySecondClass;
public MyFirstClass(IMySecondClass mySecondClass)
{
this.mySecondClass = mySecondClass;
}
public MyFirstClass() : this(new MySecondClass()){}
}
public class MySecondClass : IMySecondClass
{
MyFirstClass myFirstClass;
public MySecondClass(IMyFirstClass myFirstClass)
{
this.myFirstClass = myFirstClass;
}
public MySecondClass() : this(new MyFirstClass()){}
}
Run Code Online (Sandbox Code Playgroud)
您会注意到,当实例化这些类中的任何一个的默认构造函数时,系统将因为需要进行的无限实例化而崩溃.
是否有用于描述此问题的官方术语?
第10.6节的示例代码,预期结果是:
在几次迭代之后,getpwnam使用的静态结构将被破坏,程序将以SIGSEGV信号终止.
但在我的平台上,Fedora 11,gcc(GCC)4.4.0,结果是
[Langzi @ Freedom apue] sig_alarm
中$ ./corrupt
我只能看到sig_alarm一次输出,程序似乎因某种原因而挂起,但它确实存在,并且仍在运行.
但是当我尝试使用gdb来运行程序时,似乎没问题,我会sig_alarm定期看到输出.
从我的手册中可以看出,在处理完信号后,信号处理程序将被设置为SIG_DEF,系统不会阻塞信号.所以在我的信号处理程序的开头,我重置了信号处理程序.
也许我应该使用sigaction,但我只想知道正常运行和gdb运行之间差异的原因.
任何建议和帮助将不胜感激.
以下是我的代码:
#include "apue.h"
#include <pwd.h>
void sig_alarm(int signo);
int main()
{
struct passwd *pwdptr;
signal(SIGALRM, sig_alarm);
alarm(1);
for(;;) {
if ((pwdptr = getpwnam("Zhijin")) == NULL)
err_sys("getpwnam error");
if (strcmp("Zhijin", pwdptr->pw_name) != 0) {
printf("data corrupted, pw_name: %s\n", pwdptr->pw_name);
}
}
}
void sig_alarm(int signo)
{
signal(SIGALRM, sig_alarm);
struct passwd *rootptr;
printf("in sig_alarm\n");
if ((rootptr = getpwnam("root")) == NULL)
err_sys("getpwnam error"); …Run Code Online (Sandbox Code Playgroud) 我正在尝试在DataGrid中设置两个复选框,以便一次只能检查一个复选框.
目前,以下内容呈现屏幕上现有的选择状态:
<asp:TemplateColumn HeaderText="Choice One">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxChoiceOne"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ChoiceOne") %>'
runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Choice Two">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxChoiceTwo"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ChoiceTwo") %>'
runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
Run Code Online (Sandbox Code Playgroud)
如果用户选择ChoiceOne,任何选择的ChoiceTwo将被取消选择,反之亦然,我该如何确保?有没有办法在DataGrid控件中声明这两个复选框是否组合在一起?
我正在尝试创建一些具有这样的层次结构的虚拟对象:
Post
|
----User
Run Code Online (Sandbox Code Playgroud)
很简单.
这是代码:
var user = Builder<User>.CreateNew().Build();
var posts = Builder<Post>.CreateListOfSize(100)
.All()
.With(x => x.User == user)
.Build();
Run Code Online (Sandbox Code Playgroud)
但是对于每个项目,post.User都是null.
有任何想法吗?
我已经创建了一个简单的指南针程序,可以在屏幕上移动GUI框,现在我正在进行添加NE,NW等.
这涉及使用JPanels,因为这允许多个对象.我的问题是我为单独的按钮制作了9个单独的面板,但我不知道如何将它们添加到JFrame,因为我所做的一切似乎都不起作用.
任何想法,将不胜感激.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MovingCompassExtraJFrame extends JFrame implements ActionListener {
private JButton north;
private JButton east;
private JButton west;
private JButton south;
private JButton center;
private JButton northEast;
private JButton northWest;
private JButton southEast;
private JButton southWest;
int screenHeight;
int screenWidth;
int height;
int width;
public MovingCompassExtraJFrame()
{
super();
width = 400;
height = 300;
setSize(width, height);
setLocation(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(Color.RED);
getContentPane().setLayout(new GridLayout(3,3));
setupNEPanel();
setupNPanel();
setupNWPanel();
setupWPanel();
setupCPanel();
setupEPanel();
setupSWPanel();
setupSPanel();
setupSEPanel();
setVisible(true); …Run Code Online (Sandbox Code Playgroud)