我对Xcode(以及Mac)很新.我开始了一个小小的iPhone项目 - 尽可能用C++编写 - 并且只是注意到我的断言(); 命令也在释放模式下执行.
这是一个已知问题,如何正确解决?
谢谢!
遇到以下MS单元测试:
[TestMethod]
public void PersonRepository_AddressCountForSinglePerson_IsNotEqualToZero()
{
// Arrange.
Person person;
// Act.
person = personRepository.FindSingle(1);
// Assert.
Assert.AreNotEqual<int>(person.Addresses.Count, 0);
}
Run Code Online (Sandbox Code Playgroud)
在做断言时我从未见过使用泛型.
这是我写Assertion的方式:
// Assert.
Assert.AreNotEqual(person.Addresses.Count, 0);
Run Code Online (Sandbox Code Playgroud)
有什么不同?
当我将鼠标悬停在AreNotEqual()我正在使用的重载上时,该方法使用了比较两个双精度的重载(不确定为什么没有int,int重载).
如果我做放的泛型类型参数<int>中,ReSharper的说,这是多余的.
所以我的问题是:如果我这样做仍然是类型安全的,为什么要使用泛型断言?
我在书中读到OCJP for Java6这一部分有断言.我到达了一个部分,它概述了如果将'assert'一词用作关键字或标识符,编译器将如何反应.
a Keyword和an有identifier什么区别?任何人都可以给我一个简单的解释,另外还有一个或多个例子吗?
我想验证集合是否包含至少一个非null元素.我试过is(not(empty())),但是这通过了下面的测试.
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.not;
public class SandBoxTest {
@Test
public void shouldTestThis() {
Collection<Integer> collection = new ArrayList<Integer>();
collection.add(null);
assertThat(collection, is(not(empty())));
}
}
Run Code Online (Sandbox Code Playgroud)
有一种优雅/简单的方法吗?
不起作用的事情
@Test
public void should(){
Collection<String> collection = new ArrayList();
collection.add("gfas");
collection.add("asda");
assertThat(collection, contains(notNullValue()));
}
java.lang.AssertionError:
Expected: iterable containing [not null]
but: Not matched: "asda"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
Run Code Online (Sandbox Code Playgroud) 我正在通过休眠生成一个日期并保存在数据库中,当我得到该值并将其与插入之前的值进行比较.结果不平等!
我创建了如下日期
Date rightnow = Calendar.getInstance().getTime();
Task t1 = new Task("My task", rightnow);
taskDao.saveOrUpdate(t1);
Task taskR1 = taskDao.get(t1.getIdTask());
assertEquals("They should have to be equal dates",taskR1.getDate(),t1.getDate());
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
<2014-04-11 23:13:13.0> 与...不同 <Fri Apr 11 23:13:13 CEST 2014>
java.lang.AssertionError:
They should have to be equal dates
expected:<2014-04-11 23:13:13.0>
but was:<Fri Apr 11 23:13:13 CEST 2014>
Run Code Online (Sandbox Code Playgroud)
与问题相关的额外信息
课程任务
@Entity
@Table(name = "t_task")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idTask")
private long idTask;
...
@Column(name = "date")
private Date date; …Run Code Online (Sandbox Code Playgroud) 目前,我们正在将使用一些代码Assert.IsTrue(),Assert.AreEqual(),Assert.IsNotNull(),等的基本单元测试断言库C#
我们要使用FluentAssertions,例如 value.Should().BeNull().
我Assert.Fail()在某些地方使用了一些测试。我想用什么来有效地替换那些,因为我们要删除每个“ Assert。*”,而我在FluentAssertions中找不到等效的东西?
这是一个例子
[TestMethod, TestCategory("ImportantTest")]
public void MethodToTest_Circumstances_ExpectedResult()
{
// Arrange
var variable1 = new Type1() { Value = "hello" };
var variable2 = new Type2() { Name = "Bob" };
// Act
try
{
MethodToTest(variable1, variable2);
// This method should have thrown an exception
Assert.Fail();
}
catch (Exception ex)
{
ex.Should().BeOfType<DataException>();
ex.Message.Should().Be(Constants.DataMessageForMethod);
}
// Assert
// test that variable1 was changed by the method
variable1.Should().NotBeNull();
variable1.Value.Should().Be("Hello!");
// test …Run Code Online (Sandbox Code Playgroud) 我对Cobertura测量的单元测试的线路覆盖范围很痛苦,因为我的assert测试中没有涉及的声明.我应该测试assert离子,有没有办法让Cobertura忽略它们,这样它们不会影响我的测试覆盖率?
我的代码是
<h3 id="account_owner">This is Jhon's account</h3>
Run Code Online (Sandbox Code Playgroud)
测试此代码的行是
assert_select "h3#account_owner", "This is Jhon's account"
Run Code Online (Sandbox Code Playgroud)
测试失败并说
"这是Jhon的帐户"是预期的但是这是Jhon' s account'
我在"'"之间加了一个空格.因为它也转化为'在这个问题的输出上.有什么猜测?或者我该如何测试这条线?
我正在使用Rubberduck对我的VBA实现进行单元测试.当Assert.IsTrue在一个TestMethod中使用相同类型的多个Assert(例如)时,测试结果并没有告诉我哪个失败,据我所见.
有没有办法找出哪个Assert失败或者这是在Rubberduck未来路线图上?当然我可以添加自己的信息,例如Debug.Print在每个Assert之前使用,但这会导致很多额外的代码.
我知道在一次测试中对于多个Assert有不同的看法,但我选择在我的情况下使用它们,这个讨论已经在其他地方讨论过了 .