Class NatsFixture
Used to run a Docker nats container on the current machine as a test fixture while tests are being performed and then deletes the container when the fixture is disposed.
Implements
Inherited Members
Namespace: Neon.Xunit
Assembly: Neon.Xunit.dll
Syntax
public sealed class NatsFixture : ContainerFixture, ITestFixture
Remarks
This fixture assumes that NATS is not currently running on the local workstation or as a container named nats-test. You may see port conflict errors if either of these conditions are not true.
A somewhat safer but slower alternative, is to use the DockerFixture instead and add NatsFixture as a subfixture. The advantage is that DockerFixture will ensure that all (potentially conflicting) containers are removed before the NatsFixture fixture is started.
Use Restart() to clear the NATS server state by restarting its Docker container. This also returns the new client connection.
Constructors
NatsFixture()
Constructs the fixture.
Declaration
public NatsFixture()
Fields
ConnectionUri
Returns the URI for a NATS server running locally (probably as a Docker container).
Declaration
public const string ConnectionUri = "nats://localhost:4222"
Field Value
Type | Description |
---|---|
string |
Properties
Connection
Returns the NATS connection.
Declaration
public IConnection Connection { get; }
Property Value
Type | Description |
---|---|
IConnection |
Methods
Reset()
This method completely resets the fixture by removing and recreating the NATS container.
Declaration
public override void Reset()
Overrides
Restart()
Restarts the NATS container to clear any previous state and returns the new client connection.
Declaration
public IConnection Restart()
Returns
Type | Description |
---|---|
IConnection | The new connection. |
Start(string, string, string[], string)
Starts a NATS container if it's not already running. You'll generally want to call this in your test class constructor instead of Start(Action).
note
You'll need to call StartAsComposed(string, string, string[], string) instead when this fixture is being added to a ComposedFixture.
Declaration
public TestFixtureStatus Start(string image = null, string name = "nats-test", string[] args = null, string hostInterface = null)
Parameters
Type | Name | Description |
---|---|---|
string | image | Optionally specifies the NATS container image. This defaults to ghcr.io/neonrelease/nats:latest or ghcr.io/ghcr.io/neonrelease-dev/nats:latest depending on whether the assembly was built from a git release branch or not. |
string | name | Optionally specifies the NATS container name (defaults to |
string[] | args | Optional NATS server command line arguments. |
string | hostInterface | Optionally specifies the host interface where the container public ports will be published. This defaults to DefaultHostInterface but may be customized. This needs to be an IPv4 address. |
Returns
Type | Description |
---|---|
TestFixtureStatus | Started if the fixture wasn't previously started and this method call started it or AlreadyRunning if the fixture was already running. |
StartAsComposed(string, string, string[], string)
Used to start the fixture within a ComposedFixture.
Declaration
public void StartAsComposed(string image = null, string name = "nats-test", string[] args = null, string hostInterface = null)
Parameters
Type | Name | Description |
---|---|---|
string | image | Optionally specifies the NATS container image. This defaults to ghcr.io/neonrelease/nats:latest or ghcr.io/ghcr.io/neonrelease-dev/nats:latest depending on whether the assembly was built from a git release branch or not. |
string | name | Optionally specifies the container name (defaults to |
string[] | args | Optional NATS server command line arguments. |
string | hostInterface | Optionally specifies the host interface where the container public ports will be published. This defaults to DefaultHostInterface but may be customized. This needs to be an IPv4 address. |