#0040 增加测试用例
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using Serilog;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
using XplorePlane.Models;
|
||||
using XplorePlane.Services;
|
||||
using XplorePlane.Services.AppState;
|
||||
@@ -21,9 +22,11 @@ namespace XplorePlane.Tests.Services
|
||||
private readonly Mock<IPipelineExecutionService> _mockPipeline;
|
||||
private readonly Mock<ILogger> _mockLogger;
|
||||
private readonly RecipeService _service;
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public RecipeServiceTests()
|
||||
public RecipeServiceTests(ITestOutputHelper output)
|
||||
{
|
||||
_output = output;
|
||||
_mockAppState = new Mock<IAppStateService>();
|
||||
_mockPipeline = new Mock<IPipelineExecutionService>();
|
||||
_mockLogger = new Mock<ILogger>();
|
||||
@@ -45,6 +48,7 @@ namespace XplorePlane.Tests.Services
|
||||
public void CreateRecipe_ValidName_ReturnsEmptyRecipe()
|
||||
{
|
||||
var recipe = _service.CreateRecipe("TestRecipe");
|
||||
_output.WriteLine($"CreateRecipe('TestRecipe'): Name={recipe.Name}, Id={recipe.Id}, Steps.Count={recipe.Steps.Count}, CreatedAt={recipe.CreatedAt}");
|
||||
|
||||
Assert.Equal("TestRecipe", recipe.Name);
|
||||
Assert.Empty(recipe.Steps);
|
||||
@@ -56,19 +60,22 @@ namespace XplorePlane.Tests.Services
|
||||
[Fact]
|
||||
public void CreateRecipe_EmptyName_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => _service.CreateRecipe(string.Empty));
|
||||
var ex = Assert.Throws<ArgumentException>(() => _service.CreateRecipe(string.Empty));
|
||||
_output.WriteLine($"CreateRecipe('') threw: {ex.GetType().Name}, Message={ex.Message}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateRecipe_NullName_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => _service.CreateRecipe(null!));
|
||||
var ex = Assert.Throws<ArgumentException>(() => _service.CreateRecipe(null!));
|
||||
_output.WriteLine($"CreateRecipe(null) threw: {ex.GetType().Name}, Message={ex.Message}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateRecipe_WhitespaceName_ThrowsArgumentException()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => _service.CreateRecipe(" "));
|
||||
var ex = Assert.Throws<ArgumentException>(() => _service.CreateRecipe(" "));
|
||||
_output.WriteLine($"CreateRecipe(' ') threw: {ex.GetType().Name}, Message={ex.Message}");
|
||||
}
|
||||
|
||||
// ── RecordCurrentStep 验证 ──
|
||||
@@ -88,6 +95,7 @@ namespace XplorePlane.Tests.Services
|
||||
var pipeline = new PipelineModel { Name = "TestPipeline" };
|
||||
|
||||
var step = _service.RecordCurrentStep(recipe, pipeline);
|
||||
_output.WriteLine($"RecordCurrentStep: StepIndex={step.StepIndex}, MotionState.XM={step.MotionState.XM}, RaySource.Voltage={step.RaySourceState.Voltage}, Detector.Resolution={step.DetectorState.Resolution}, Pipeline={step.Pipeline.Name}");
|
||||
|
||||
Assert.Equal(0, step.StepIndex);
|
||||
Assert.Same(motionState, step.MotionState);
|
||||
@@ -102,9 +110,11 @@ namespace XplorePlane.Tests.Services
|
||||
public async Task LoadAsync_FileNotExists_ThrowsFileNotFoundException()
|
||||
{
|
||||
var nonExistentPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".json");
|
||||
_output.WriteLine($"LoadAsync 不存在的文件: {nonExistentPath}");
|
||||
|
||||
await Assert.ThrowsAsync<FileNotFoundException>(
|
||||
var ex = await Assert.ThrowsAsync<FileNotFoundException>(
|
||||
() => _service.LoadAsync(nonExistentPath));
|
||||
_output.WriteLine($"抛出异常: {ex.GetType().Name}, Message={ex.Message}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -114,9 +124,11 @@ namespace XplorePlane.Tests.Services
|
||||
try
|
||||
{
|
||||
await File.WriteAllTextAsync(tempFile, "{ this is not valid json !!! }");
|
||||
_output.WriteLine($"LoadAsync 无效JSON文件: {tempFile}");
|
||||
|
||||
await Assert.ThrowsAsync<InvalidDataException>(
|
||||
var ex = await Assert.ThrowsAsync<InvalidDataException>(
|
||||
() => _service.LoadAsync(tempFile));
|
||||
_output.WriteLine($"抛出异常: {ex.GetType().Name}, Message={ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user