-
Notifications
You must be signed in to change notification settings - Fork 98
Unit testing without a router
TikFakeConnection stands in for a real ITikConnection so your tests run in CI with no hardware. It
intercepts at CallCommandSync, which means the whole call stack above it — commands, parameters and the
O/R mapper — runs through real library code, and only the wire is replaced. Use it to test the code that
uses tik4net; to watch a real exchange instead, see Communication debugging.
⚠️ Alpha — ships inv4.0.0-alpha: tested and functional, but the API may still change before the final 4.0 release. See Connection types & capabilities.
Add the tik4net.testing NuGet package to your test project:
dotnet add package tik4net.testing
var conn = new TikFakeConnection()
.WithEntities(new IpAddress { Address = "10.0.0.1/24", Interface = "ether1" }.WithId("*1"))
.WithScalarResponse(rows => rows.First() == "/ip/address/add", "*2")
.WithNonQuery(rows => rows.First() == "/ip/address/set");
var list = conn.LoadAll<IpAddress>();
Assert.AreEqual(1, list.Count());The fake works at all three levels, and each has its own page of recipes:
-
Testing low-level API —
CallCommandSync, raw sentences -
Testing mid-level API —
ITikCommand.ExecuteList,ExecuteScalar,ExecuteAsync, … -
Testing high-level API —
LoadAll,Save,Delete,SaveListDifferences, …
TikFakeConnection declares Crud | Listen | AsyncCommands | SafeMode — the third means code that awaits
Execute*Async can be unit-tested against it. The fake's
commands complete synchronously, so an await returns immediately and the test needs no timing tricks.
It also implements ITikRawSentenceConnection (so CallCommandSync works) and ITikSafeModeConnection
(SafeModeTake/Release/Unroll/Get are recorded via counters, not performed against a real router —
see Safe Mode); it does not implement ITikTaggedConnection, since a fake has no wire to tag.
Assign Capabilities to test the other branch — what your code does against a transport that lacks something:
var conn = new TikFakeConnection { Capabilities = TikConnectionCapability.Crud };
// cmd.ExecuteListAsync() now throws TikConnectionCapabilityNotSupportedException here,
// exactly as it would over Telnet — so the synchronous fallback gets exercised.The default set is deliberately not "everything": a fake that claims every capability cannot be used to test
the fail-closed paths, and claiming one it does not implement (RawCommand) would only move the failure
later. Note it does not report CancelInFlight either — there is no in-flight request to abort. Clear
SafeMode from Capabilities to test the branch where a transport has no safe mode (e.g. REST).
- Communication debugging — watching a real exchange: protocol words and raw wire bytes
- Connection types & capabilities — what each real transport declares
- Exception handling — the exceptions your tests will assert on