using Util.Domain.Tests.Samples;
using Util.Domain.Tests.XUnitHelpers;
using Util.Exceptions;
using Xunit;
namespace Util.Domain.Tests.Entities;
///
/// string标识实体测试
///
public class StringEntityTest {
///
/// 测试初始化
///
public StringEntityTest() {
_sample = new StringAggregateRootSample();
_sample2 = new StringAggregateRootSample();
}
///
/// 聚合根测试样例
///
private StringAggregateRootSample _sample;
///
/// 聚合根测试样例2
///
private StringAggregateRootSample _sample2;
///
/// 测试实体相等性 - 当两个实体的标识相同,则实体相同
///
[Fact]
public void TestEquals_IdEquals() {
_sample = new StringAggregateRootSample( "a" );
_sample2 = new StringAggregateRootSample( "a" );
Assert.True( _sample.Equals( _sample2 ) );
Assert.True( _sample == _sample2 );
Assert.False( _sample != _sample2 );
}
///
/// 测试实体相等性 - Id为空
///
[Fact]
public void TestEquals_Id_Null() {
_sample = new StringAggregateRootSample( null );
_sample2 = new StringAggregateRootSample( "a" );
Assert.False( _sample.Equals( _sample2 ) );
Assert.False( _sample == _sample2 );
Assert.False( _sample2 == _sample );
_sample = new StringAggregateRootSample( "a" );
_sample2 = new StringAggregateRootSample( null );
Assert.False( _sample.Equals( _sample2 ) );
Assert.False( _sample == _sample2 );
Assert.False( _sample2 == _sample );
}
///
/// 测试验证 - Id为空,无法通过
///
[Fact]
public void TestValidate_IdIsEmpty() {
AssertHelper.Throws( () => {
_sample = new StringAggregateRootSample( null );
_sample.Validate();
}, "标识" );
AssertHelper.Throws( () => {
_sample = new StringAggregateRootSample( "" );
_sample.Validate();
}, "标识" );
}
}