public class ControllerTest
{
private readonly IFixture fixure;
private readonly Mock<ICustomerService> service;;
private readonly CustController controller;
public ControllerTest()
{
service = new Mock<ICustomerService>();
controller = new CustController(service.Object);
fixure = new Fixture();
}
[Fact]
public async Task Controller _GetCustByIdName_ReturnOk()
{
//arrange
var customers = fixure.Create<IEnumerable<EmployeeDto>>();
var id = fixure.Create<string>();
var name = fixure.Create<string>();
service.Setup(x => x.GetCustomers(id,name)).ReturnsAsync(customers);
}
}
I have above code to unit test my method which accepts id and name and return list of employees. I am getting the error on last line of service.SetuUp as
isetup does not contain definition for returnsasync
Am I missing something?
Related
I am getting a below error while unit testing the piece of code
NSubstitute.Exceptions.ReceivedCallsException: Expected to receive a call matching error while unit testing
NSubstitute.Exceptions.ReceivedCallsException
HResult=0x80131500
Message=Expected to receive a call matching:
enrollWithHelper("", "")
Actually received no matching calls.
Source=NSubstitute
StackTrace:
at NSubstitute.Core.ReceivedCallsExceptionThrower.Throw(ICallSpecification callSpecification, IEnumerable1 matchingCalls, IEnumerable1 nonMatchingCalls, Quantity requiredQuantity)
at NSubstitute.Routing.Handlers.CheckReceivedCallsHandler.Handle(ICall call)
at NSubstitute.Routing.Route.Handle(ICall call)
at NSubstitute.Proxies.CastleDynamicProxy.CastleForwardingInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ObjectProxy_1.enrollWithHelper(String name, String type)
at MyProject.MyTest.processing_test_pass() in C:\MyProject\MyTest.cs:line 75
Framework: .NET Core
Unit Testing: NSubstitute
Here is my class under test
public class MyService: IMysService
{
private readonly IRepository _repository;
Private readonly IIoTHelper _iotHelper;
Private readonly HttpClient _httpClient;
private Configuration _configuration;
public MyService(IRepository repository, IIoTHelper iotHelper, HttpClient httpClient )
{
_repository = repository;
_iotHelper =iotHelper;
_httpClient = httpClient ;
}
public bool CallService(JObject reqObj, out Status status)
{
bool provisioningSuccess = false;
return PreProcessing(reqObj,out Status status); //private method
}
}
Here is my private method
private PreProcessing(JObject JosnObj, out Status status)
{
if (_configuration== null)
{
_configuration= _repository.GetConfigurations()
}
using (var client = this._httpClient)
{
var request = new {_configuration.Number,_configuration.Type};
var response = client.PostAsJsonAsync("api/preprocess", request).Result;
if (response.IsSuccessStatusCode)
{
_iotHelper.enrollWithHelper(_configuration.Number,_configuration.Type);
}
}
}
Here is my Configuration class
public class Configuration
{
public Configuration (string number, string type)
{
Number= number;
Type= type;
}
public string Number { get;}
public string Type { get; }
}
Here is my unit test code, where I want to make sure it reaches the private method
protected override void InitializeTest()
{
this._repository = Substitute.For<IRepository>();
this._iotHelper = Substitute.For<IIotHelper>();
}
[TestMethod]
public void processing_test_pass()
{
//Arrange
var messageHandler = new MockHttpMessageHandler("TEST VALUE", HttpStatusCode.OK);
var httpClient = new HttpClient(messageHandler);
JObject reqobj= new JObject(
new JProperty("user", "username"),
new JProperty("pass", "password"));
Status status = new Status();
//act
var t = new MyService(this._repository,this._iotHelper, httpClient );
bool success = t.CallService(reqobj, out status);
//assert
this._iotHelper.Received().enrollWithHelper("","");
}
How would I test enrollWithHelper is called?
Also I am stuck due to error!
I am using MassTransit 5.5.5 version and xunit 2.4.1
My consumer looks like this
public class StorageInformationConsumer : IConsumer<CreateStorageInformationSummary>
{
private readonly IBus _serviceBus;
private readonly USIntegrationQueueServiceContext _USIntegrationQueueServiceContext;
public StorageInformationConsumer(IBus serviceBus, USIntegrationQueueServiceContext USIntegrationQueueServiceContext)
{
_serviceBus = serviceBus;
_USIntegrationQueueServiceContext = USIntegrationQueueServiceContext;
}
public async Task Consume(ConsumeContext<CreateStorageInformationSummary> createStorageInformationSummarycontext)
{
//....
}
}
And my Test like this
public class StorageInformationConsumerTest
{
private readonly USIntegrationQueueServiceContext _dbContext;
private readonly Mock<IBus> _serviceBusMock;
private readonly StorageInformationConsumer _storageInformationConsumer;
public StorageInformationConsumerTest()
{
var options = new DbContextOptionsBuilder<USIntegrationQueueServiceContext>()
.UseInMemoryDatabase(databaseName: "InMemoryArticleDatabase")
.Options;
_dbContext = new USIntegrationQueueServiceContext(options);
_serviceBusMock = new Mock<IBus>();
_storageInformationConsumer = new StorageInformationConsumer(_serviceBusMock.Object, _dbContext);
}
[Fact]
public async void ItShouldCreateStorageInformation()
{
var createStorageInformationSummary = new CreateStorageInformationSummary
{
ClaimNumber = "C-1234",
WorkQueueItemId = 1,
StorageInformation = CreateStorageInformation(),
};
//How to consume
}
}
How to consume the CreateStorageInformationSummary message in order to call consumer, following doesn't work
var mockMessage = new Mock<ConsumeContext<CreateStorageInformationSummary>>(createStorageInformationSummary);
await _storageInformationConsumer.Consume(mockMessage.Object);
Since you have not clarified what is actually not working, the most I can provide is how to create the mock context and pass it to the subject method under test.
This is simple enough since ConsumeContext<T> is already an interface
[Fact]
public async Task ItShouldCreateStorageInformation() {
//Arrange
var createStorageInformationSummary = new CreateStorageInformationSummary {
ClaimNumber = "C-1234",
WorkQueueItemId = 1,
StorageInformation = CreateStorageInformation(),
};
//Mock the context
var context = Mock.Of<ConsumeContext<CreateStorageInformationSummary>>(_ =>
_.Message == createStorageInformationSummary);
//Act
await _storageInformationConsumer.Consume(context);
//Assert
//...assert the expected behavior
}
Also take note that the test has been updated to return async Task and not async void
Reference Moq Quickstart
I have created small kind of xunit test case but I don't know how to create this controller which i have mention below.
public class PropertyController : ControllerBase
{
private readonly IMediator _mediator;
private readonly ILogger<PropertyController> _logger;
public PropertyController(IMediator mediator, ILogger<PropertyController> logger)
{
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<IActionResult> AddProperty([FromBody] AddPropertyCommand command)
{
bool commandResult = false;
_logger.LogInformation(
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({#Command})",
command.GetGenericTypeName(),
nameof(command.ModifiedUserId),
command.ModifiedUserId,
command);
commandResult = await _mediator.Send(command);
if (!commandResult)
{
return BadRequest();
}
return Ok();
}
I have created like this. i have mock the dependency and create a test case for add command is working fine or not
public class PropertyControllerTest
{
private readonly PropertyController _it;
private readonly Mock<IMediator> _mediatorMock;
private readonly Mock<ILogger<PropertyController>> _loggerPropertycontrollerMock;
public PropertyControllerTest()
{
_mediatorMock = new Mock<IMediator>();
_loggerPropertycontrollerMock = new Mock<ILogger<PropertyController>>();
_it = new PropertyController(_mediatorMock.Object, _loggerPropertycontrollerMock.Object);
}
[Fact]
public void it_Should_add_information_successfully_and_returns_200_status_result()
{
//How can i write xunit test case. I'm creating like this
_mediatorMock.Setup(x => x.Send().Returns(property);
}
The test below covers the 200 status result - a similar test for bad requests would be very similar.
[Fact]
public void it_Should_add_information_successfully_and_returns_200_status_result()
{
// Arrange
var expected = new AddPropertyCommand();
_mediatorMock.Setup(x => x.Send(It.IsAny<AddPropertyCommand>())).Returns(true);
// Act
var actionResult = _it.AddProperty(expected);
// Assert
actionResult.ShouldBeAssignableTo<OkResult>();
_mediatorMock.Verify(x => x.Send(expected));
}
N.B. actionResult.ShouldBeAssignableTo<OkResult>(); is written using the Shouldly assertion framework, you can swap that out for anything you like. The one built into XUnit would be like this: Assert.IsType(typeof(OkResult), actionResult);
I an getting the below issue when running the Unit Test project.
Unable to get Default Constructor For class ********
[TestClass]
public class PersonRegistration
{
private ILoggingService _loggingService;
private IUserManager _userManager;
public PersonRegistration(IUserManager userManager, ILoggingService loggingService)
{
this._userManager = userManager;
this._loggingService = loggingService;
}
[TestMethod]
public void TestMethod1()
{
RegisterBindingModel model = new RegisterBindingModel();
AccountController ac = new AccountController(_userManager, _loggingService);
model.UserName = "test123#gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
How to fix that. Some answers says that to use a parameter less constructor. But here I need params.
RegisterBindingModel()
public class RegisterBindingModel
{
public RegisterBindingModel();
[Display(Name = "User name")]
[Required]
public string UserName { get; set; }
}
Issue
I've just tested this in my unit tests.
Add
public PersonRegistration()
{
}
And it should run fine.
There is no need for constructors on your unit test classses. If you are using a mocking framework like Moq then I use a factory to return the dependent moqs for the classes I'm testing.
public ILoggingService ReturnMockLoggingService()
{
var mockService = new Mock<ILoggingService>();
return mockService.Object;
}
Then in the test fixture.
[TestMethod]
public void TestMethod1()
{
RegisterBindingModel model = new RegisterBindingModel();
var logService = MockFactory.ReturnMockLoggingService();
var userService = MockFactory.ReturnMockUserService();
AccountController ac = new AccountController(userService, logService);
model.UserName = "test123#gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
if you're not using mocks then simply instance the user and log service in the test or create a SetUp.
[ClassInitialize]
public void SetUp()
{
_loggingService = new LoggingService();
_userManager = new UserManager();
}
Hope that helps.
You should use a mocking framework like Moq.
Example:
[TestClass]
public class PersonRegistration
{
[TestMethod]
public void TestMethod()
{
RegisterBindingModel model = new RegisterBindingModel();
var mockService = new Mock<ILoggingService>();//Mock
//Do something as per your requirement
//var reg= new List<RegisterBindingModel >(); // provide some sample list
//mockService .Setup(r => r.GetAll=()).Return(reg);
var mockManager = new Mock<IUserManager>();//Mock
//Do something as per your requirement
//var user= new List<User>(); // provide some sample list
//mockManager .Setup(r => r.GetAll=()).Return(user);
AccountController ac = new AccountController(mockManager.Object, mockService.Object);
model.UserName = "test123#gmail.com";
var result = ac.Register(model);
Assert.AreEqual("User Registered Successfully", result);
}
}
You can get help form this and this link.
I have controller with localization
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
public IActionResult Index()
{
ViewData["MyTitle"] = _localizer["Hello my dear friend!"];
return View("Index");
}
}
and I added xUnit project for testing and wrote next code
public class HomeControllerTest
{
private readonly IStringLocalizer<HomeController> _localizer;
private HomeController _controller;
private ViewResult _result;
public HomeControllerTest()
{
_controller = new HomeController(_localizer);
_result = _controller.Index() as ViewResult;
}
[Fact]
public void IndexViewDataMessage()
{
// Assert
Assert.Equal("Hello my dear friend!", _result?.ViewData["MyTitle"]);
}
[Fact]
public void IndexViewResultNotNull()
{
// Assert
Assert.NotNull(_result);
}
[Fact]
public void IndexViewNameEqualIndex()
{
// Assert
Assert.Equal("Index", _result?.ViewName);
}
}
When I running all tests, they returns false with exception:
Message: System.NullReferenceException : Object reference not set to
an instance of an object.
When you double-click on a method in the StackTrace cursor appears on the line
ViewData["MyTitle"] = _localizer["Hello my dear friend!"];
I think this is due to IStringLocalizer. How to fix it? May be somebody knows what is the reason?
Setup the mock to return your expected result.
var mock = new Mock<IStringLocalizer<HomeController>>();
string key = "Hello my dear friend!";
var localizedString = new LocalizedString(key, key);
mock.Setup(_ => _[key]).Returns(localizedString);
_localizer = mock.Object;
_controller = new HomeController(_localizer);
If you need strings from the actual localized resources in your tests you can add the Microsoft.AspNetCore.All Nuget package to your test project and then use the following code:
var options = Options.Create(new LocalizationOptions {ResourcesPath = "Resources"});
var factory = new ResourceManagerStringLocalizerFactory(options, NullLoggerFactory.Instance);
var localizer = new StringLocalizer<HomeController>(factory);
The ResourcesPath should be the relative path of where HomeController.en.resx is found from the project root.