Skip to content

Commit 8d78873

Browse files
committed
test: 更新单元测试
1 parent 410f8b8 commit 8d78873

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

src/BootstrapBlazor/Services/Serial/DefaultSerialService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class DefaultSerialService : ISerialService, IAsyncDisposable
1313

1414
private JSModule? _module;
1515

16-
private IJSRuntime _runtime;
16+
private readonly IJSRuntime _runtime;
1717

18-
private string _serialPortId;
18+
private readonly string _serialPortId;
1919

2020
private SerialPort? _serialPort;
2121

src/BootstrapBlazor/Services/Serial/SerialSignals.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5-
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Text;
9-
using System.Threading.Tasks;
10-
115
namespace BootstrapBlazor.Components;
126

137
//RS-232C接口定义(DB9)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace UnitTest.Services;
6+
7+
public class SerialServiceTest : BootstrapBlazorTestBase
8+
{
9+
[Fact]
10+
public async Task GetPort_Ok()
11+
{
12+
Context.JSInterop.Setup<bool>("init", matcher => matcher.Arguments.Count == 1 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true);
13+
Context.JSInterop.Setup<bool>("getPort", matcher => matcher.Arguments.Count == 1 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true);
14+
Context.JSInterop.Setup<bool>("open", matcher => matcher.Arguments.Count == 4 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true);
15+
Context.JSInterop.Setup<bool>("close", matcher => matcher.Arguments.Count == 1 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true);
16+
Context.JSInterop.Setup<bool>("write", matcher => matcher.Arguments.Count == 2 && (matcher.Arguments[0]?.ToString()?.StartsWith("bb_serial_") ?? false)).SetResult(true);
17+
var serialService = Context.Services.GetRequiredService<ISerialService>();
18+
19+
var serialPort = await serialService.GetPort();
20+
Assert.True(serialService.IsSupport);
21+
Assert.NotNull(serialPort);
22+
23+
// DataReceive
24+
serialPort.DataReceive = d =>
25+
{
26+
return Task.CompletedTask;
27+
};
28+
29+
var option = new SerialOptions()
30+
{
31+
BaudRate = 9600,
32+
BufferSize = 1024,
33+
DataBits = 8,
34+
FlowControlType = SerialFlowControlType.Hardware,
35+
ParityType = SerialParityType.Odd,
36+
StopBits = 1
37+
};
38+
await serialPort.Open(option);
39+
Assert.True(serialPort.IsOpen);
40+
Assert.Equal(9600, option.BaudRate);
41+
Assert.Equal(1024, option.BufferSize);
42+
Assert.Equal(8, option.DataBits);
43+
Assert.Equal(SerialFlowControlType.Hardware, option.FlowControlType);
44+
Assert.Equal(SerialParityType.Odd, option.ParityType);
45+
Assert.Equal(1, option.StopBits);
46+
47+
await serialPort.Write([0x31, 0x32]);
48+
49+
var mi = serialPort.GetType().GetMethod("DataReceiveCallback");
50+
Assert.NotNull(mi);
51+
mi.Invoke(serialPort, ["12"u8.ToArray()]);
52+
53+
await serialPort.Close();
54+
Assert.False(serialPort.IsOpen);
55+
56+
await serialPort.DisposeAsync();
57+
}
58+
}

0 commit comments

Comments
 (0)