I have a class and in each method I am declaring the following lines repeatedly:
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(#"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
How and where do I declare them so that it is available to each method so that I'm not having to write the same line repeatedly inside each method?
public class EmailService
{
public EmailService()
{
}
public void NotifyNewComment(int id)
{
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(#"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var email = new NotificationEmail
{
To = "yourmail#example.com",
Comment = comment.Text
};
email.Send();
}
public void NotifyUpdatedComment(int id)
{
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(#"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
var email = new NotificationEmail
{
To = "yourmail#example.com",
Comment = comment.Text
};
email.Send();
}
}
You could make them class-level members:
public class EmailService
{
private string viewsPath;
private ViewEngineCollection engines;
public EmailService()
{
viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(#"~/Views/Emails"));
engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
}
public void NotifyNewComment(int id)
{
var email = new NotificationEmail
{
To = "yourmail#example.com",
Comment = comment.Text
};
email.Send();
}
// etc.
}
This would populate the variables once when you create a new EmailService:
new EmailService()
Then any method executed on that instance would use the values which were created at that time.
Related
I am trying to write down unit test for my repository which is interacting with MongoDb but facing a problem/exception in mocking the objects. This is what I have tried so far;
This is how my class looks like i.e. I have create a repository class and extend it from interface;
public class UserManagementRepository : IUserManagementRepository
{
private readonly IMongoCollection<UserModel> _users;
public UserManagementRepository(IDatabaseSettings dbSettings, IApplicationSettings applicationSettings, IMongoClient mongoClient)
{
IMongoDatabase database = mongoClient.GetDatabase(dbSettings.DatabaseName);
_users = database.GetCollection<UserModel>(applicationSettings.UserCollectionName);
}
public async Task<GeneralResponse> Get(string id)
{
GeneralResponse response = new GeneralResponse();
try
{
IAsyncCursor<UserModel> userModel = await _users.FindAsync(user => user.Id == id);
if (userModel != null)
{
response.Message = "User exists!";
response.Data = userModel.FirstOrDefault();
response.ResponseCode = ResponseCode.Success;
}
else
{
response.Message = $"User with Id: {id} not found!";
response.ResponseCode = ResponseCode.Success;
}
}
catch (Exception ex)
{
response.Message = "Failure";
response.ResponseCode = ResponseCode.Error;
}
return response;
}
}
This is how my Test class look like
public class UserManagmentRepositoryTests
{
private Mock<IDatabaseSettings> _mockDbSettings;
private Mock<IApplicationSettings> _mockApplicationSettings;
private Mock<IMongoClient> _mockClient;
public UserManagmentRepositoryTests()
{
_mockDbSettings = new Mock<IDatabaseSettings>();
_mockApplicationSettings = new Mock<IApplicationSettings>();
_mockClient = new Mock<IMongoClient>();
}
[Fact]
public async Task GetUserWithId_Test()
{
// Arrange
var repo = new Mock<IUserManagementRepository>();
IDatabaseSettings dbSettings = new DatabaseSettings()
{
ConnectionString = "mongodb:connectionstring",
DatabaseName = "dbname"
};
_mockDbSettings.Setup(x => x).Returns(dbSettings);
IApplicationSettings applicationSettings = new ApplicationSettings() { UserCollectionName = "users" };
_mockApplicationSettings.Setup(app => applicationSettings).Returns(applicationSettings);
MongoClientSettings clientSettings = MongoClientSettings.FromConnectionString(dbSettings.ConnectionString);
MongoClient client = new MongoClient(clientSettings);
_mockClient.Setup(c => client);
var ctr = new UserManagementRepository(_mockDbSettings.Object, _mockApplicationSettings.Object, _mockClient.Object);
// Act
var result = ctr.Get("132");
// Assert
//result.StatusCode.Should().NotBeNull();
}
}
I get an exception on every setup
You are trying to use ApplicationSettings as an expression. The setup method expects you to provide an expression for when to activate the mocked override.
In your case just get rid of the Mock<IApplicationSettings> altogether and mock all the objects you need to call any methods on:
[Fact]
public async Task GetUserWithId_Test()
{
// Arrange
var userModel = new UserModel() { Id = "123" };
var mockCursor = new Mock<IAsyncCursor<UserModel>>();
mockCursor
.Setup(c => c.FirstOrDefault())
.Returns(userModel);
var mockCollection = new Mock<IMongoCollection<UserModel>>();
Predicate<UserModel> filter = u => u.Id == userModel.Id;
mockCollection
.Setup(c => c.FindAsync(filter))
.ReturnsAsync(mockCursor.Object);
var mockDb = new Mock<IMongoDatabase>();
mockDb
.Setup(db => db.GetCollection<UserModel>(It.IsAny<string>()))
.Returns(mockCollection.Object);
_mockClient
.Setup(c => c.GetDatabase(It.IsAny<string>()))
.Returns(mockDb.Object)
var ctr = new UserManagementRepository(new(), new(), _mockClient.Object);
// Act
var result = ctr.Get("132");
// Assert
result.StatusCode.Should().Be(ResponseCode.Success);
result.Message.Should().Be("User exists!");
result.Data.Should().Be(userModel);
}
Hope this helps a little 🙂
To use your ApplicationSettings as mocked ApplicationSettings, you must use an expression in the setup method.
Instead of:
IApplicationSettings applicationSettings = new ApplicationSettings() { UserCollectionName = "users" };
_mockApplicationSettings.Setup(app => applicationSettings).Returns(applicationSettings);
Use e.g. this:
IApplicationSettings applicationSettings = new ApplicationSettings() { UserCollectionName= "users" };
_mockApplicationSettings.Setup(app => app.UserCollectionName).Returns(applicationSettings.UserCollectionName);
Edit: Complete Example
private Mock<IDatabaseSettings> _mockDbSettings;
private Mock<IApplicationSettings> _mockApplicationSettings;
private Mock<IMongoClient> _mockClient;
public UserManagmentRepositoryTests()
{
_mockDbSettings = new Mock<IDatabaseSettings>();
_mockApplicationSettings = new Mock<IApplicationSettings>();
_mockClient = new Mock<IMongoClient>();
}
[Fact]
public async Task GetUserWithId_Test()
{
// Arrange
var repo = new Mock<IUserManagementRepository>();
IDatabaseSettings dbSettings = new DatabaseSettings()
{
ConnectionString = "mongodb:connection",
DatabaseName = "dbname"
};
_mockDbSettings.Setup(x => x.ConnectionString).Returns(dbSettings.ConnectionString);
_mockDbSettings.Setup(x => x.DatabaseName).Returns(dbSettings.DatabaseName);
IApplicationSettings applicationSettings = new ApplicationSettings() { UserCollectionName = "users" };
_mockApplicationSettings.Setup(app => app.UserCollectionName).Returns(applicationSettings.UserCollectionName);
MongoClientSettings clientSettings = MongoClientSettings.FromConnectionString(dbSettings.ConnectionString);
MongoClient client = new MongoClient(clientSettings);
_mockClient.Setup(c => c.GetDatabase(It.IsAny<string>(), It.IsAny<MongoDatabaseSettings>()))
.Returns <string, MongoDatabaseSettings>((name, settings) => client.GetDatabase(name, settings));
var ctr = new UserManagementRepository(_mockDbSettings.Object, _mockApplicationSettings.Object, _mockClient.Object);
// Act
var result = await ctr.Get("132");
// Assert
result.ResponseCode.Should().NotBe(ResponseCode.Error);
}
I see no reason mocking IApplicationSettings and IDatabaseSettings you should go with :
public class UserManagmentRepositoryTests
{
private Mock<IMongoClient> _mockClient;
public UserManagmentRepositoryTests()
{
_mockClient = new Mock<IMongoClient>();
}
[Fact]
public async Task GetUserWithId_Test()
{
// Arrange
var repo = new Mock<IUserManagementRepository>();
IDatabaseSettings dbSettings = new DatabaseSettings()
{
ConnectionString = "mongodb:connectionstring",
DatabaseName = "dbname"
};
IApplicationSettings applicationSettings = new ApplicationSettings() { UserCollectionName = "users" };
MongoClientSettings clientSettings = MongoClientSettings.FromConnectionString(dbSettings.ConnectionString);
MongoClient client = new MongoClient(clientSettings);
_mockClient.Setup(c => client);
var ctr = new UserManagementRepository(dbSettings, applicationSettings, _mockClient.Object);
// Act
var result = ctr.Get("132");
// Assert
//result.StatusCode.Should().NotBeNull();
}
}
I am attempting to write a unit test for this method:
public List<Annotation> GetNotesByOrderGuid(Guid orderGuid)
{
var result = _xrmServiceContext.SalesOrderSet.Where(x => x.Id == orderGuid); //!!!!! this is returning correctly 1 record, however, it shows NULL for the list of annotations
//do some stuff and return a list of annotations
}
My unit test creates 2 notes, and attaches them to the sales order:
private XrmFakedContext _fakeContext;
[NotNull]
private IOrganizationService _fakeOrganizationService;
[Test]
public void GetNotesByOrderGuid_ExistingRecordHavingNotes_ReturnsListOfThoseNotes()
{
using (var xrmServiceContext = new XrmServiceContext(_fakeOrganizationService))
{
// Arrange
var salesOrderGuid = Guid.NewGuid();
var salesOrder = new SalesOrder { Id = salesOrderGuid };
var note1 = new Annotation
{
Id = Guid.NewGuid(),
NoteText = "this is note1",
ObjectId = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
ObjectTypeCode = salesOrder.LogicalName
};
var note2 = new Annotation
{
Id = Guid.NewGuid(),
NoteText = "this is note2",
ObjectId = new EntityReference(SalesOrder.EntityLogicalName, salesOrderGuid),
ObjectTypeCode = salesOrder.LogicalName
};
_fakeContext.Initialize(new List<Entity> { salesOrder, note1, note2});
var sut = new SalesOrderService(xrmServiceContext);
// Act
// Assert
Assert.That(sut.GetNotesByOrderGuid(salesOrderGuid), Is.InstanceOf<List<Annotation>>());
}
}
[SetUp]
public void Init()
{
_fakeContext = new XrmFakedContext { ProxyTypesAssembly = Assembly.GetAssembly(typeof(SalesOrder)) };
_fakeOrganizationService = _fakeContext.GetFakedOrganizationService();
}
What am doing incorrectly in adding annotations to my entity?
The reason I ask is because when the unit tests runs this code:
var result = _xrmServiceContext.SalesOrderSet.Where(x => x.Id == orderGuid);
it shows that although there is 1 result, as there correctly should be; it shows that no annotations have been linked to it:
I want to declare the following things:
var simlBot = new SimlBot();
var botUser = simlBot.CreateUser();
var packageString = File.ReadAllText("SIMLPackage.simlpk");
simlBot.PackageManager.LoadFromString(packageString);
If I declare it directly in the class I get: "The contextual keyword 'var' may only appear within a local variable declaration or in script code"
I can't declare it inside an if statement because I can't access it from outside.
if (hasBotBeenCreated == false)
{
var simlBot = new SimlBot();
var botUser = simlBot.CreateUser();
var packageString = File.ReadAllText("SIMLPackage.simlpk");
simlBot.PackageManager.LoadFromString(packageString);
}
I need to use the following form inside a function:
var chatRequest = new ChatRequest(textReceived, botUser);
var chatResult = simlBot.Chat(chatRequest);
What can I do? The problem is that the last snippet of code can't access botUser and simlBot. The way I have it working now is recreating the object every time the function is called, but that takes a lot of time.
My complete code is
public partial class Form1 : Form
{
string textEntered = "";
string response = "";
SpeechSynthesizer synth = new SpeechSynthesizer();
bool hasBotBeenCreated = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textEntered = textBox1.Text;
textBox1.Text = "";
if (textEntered != "")
{
sendToAI(textEntered);
}
}
void sendToAI(string textReceived)
{
listBox1.Items.Add(textEntered);
response = getResponse(textEntered);
listBox1.Items.Add(response);
synth.Speak(response);
}
string getResponse(string textReceived)
{
if (hasBotBeenCreated == false)
{
var simlBot = new SimlBot();
var botUser = simlBot.CreateUser();
var packageString = File.ReadAllText("SIMLPackage.simlpk");
simlBot.PackageManager.LoadFromString(packageString);
}
var chatRequest = new ChatRequest(textReceived, botUser);//These two can't access the objects created above
var chatResult = simlBot.Chat(chatRequest);
if (chatResult.Success)
{
var botMessage = chatResult.BotMessage;
return botMessage;
}
else
{
return "I don't have a response for that";
}
}
}
Revision 2:
string getResponse(string textReceived)
{
SimlBot simlBot;
BotUser botUser;
if (hasBotBeenCreated == false)
{
simlBot = new SimlBot();
botUser = simlBot.CreateUser();
var packageString = File.ReadAllText("SIMLPackage.simlpk");
simlBot.PackageManager.LoadFromString(packageString);
}
var chatRequest = new ChatRequest(textReceived, botUser); //These get the error
var chatResult = simlBot.Chat(chatRequest);
if (chatResult.Success)
{
var botMessage = chatResult.BotMessage;
return botMessage;
}
else
{
return "I don't have a response for that";
}
}
Add the members:
SimlBot simlBot;
WhatEverTypeOfCreateUserIs botUser;
Then initialize them:
if (hasBotBeenCreated == false)
{
simlBot = new SimlBot();
botUser = simlBot.CreateUser();
....
}
---------- Full Code ----------
public partial class Form1 : Form
{
string textEntered = "";
string response = "";
SpeechSynthesizer synth = new SpeechSynthesizer();
bool hasBotBeenCreated = false;
SimlBot simlBot;
BotUser botUser;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textEntered = textBox1.Text;
textBox1.Text = "";
if (textEntered != "")
{
sendToAI(textEntered);
}
}
void sendToAI(string textReceived)
{
listBox1.Items.Add(textEntered);
response = getResponse(textEntered);
listBox1.Items.Add(response);
synth.Speak(response);
}
string getResponse(string textReceived)
{
if (hasBotBeenCreated == false)
{
simlBot = new SimlBot();
botUser = simlBot.CreateUser();
var packageString = File.ReadAllText("SIMLPackage.simlpk");
simlBot.PackageManager.LoadFromString(packageString);
}
var chatRequest = new ChatRequest(textReceived, botUser);//These two can't access the objects created above
var chatResult = simlBot.Chat(chatRequest);
if (chatResult.Success)
{
var botMessage = chatResult.BotMessage;
return botMessage;
}
else
{
return "I don't have a response for that";
}
}
}
I would like to pass an object and expression into a dynamically created workflow to mimic the Eval function found in many languages. Can anyone help me out with what I am doing wrong? The code below is a very simple example if taking in a Policy object, multiple its premium by 1.05, then return the result. It throws the exception:
Additional information: The following errors were encountered while processing the workflow tree:
'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error: Value for a required activity argument 'To' was not supplied.
And the code:
using System.Activities;
using System.Activities.Statements;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Policy p = new Policy() { Premium = 100, Year = 2016 };
var inputPolicy = new InArgument<Policy>();
var theOutput = new OutArgument<object>();
Activity dynamicWorkflow = new DynamicActivity()
{
Properties =
{
new DynamicActivityProperty
{
Name="Policy",
Type=typeof(InArgument<Policy>),
Value=inputPolicy
}
},
Implementation = () => new Sequence()
{
Activities =
{
new Assign()
{
To = theOutput,
Value=new InArgument<string>() { Expression = "Policy.Premium * 1.05" }
}
}
}
};
WorkflowInvoker.Invoke(dynamicWorkflow);
}
}
public class Policy
{
public int Premium { get; set; }
public int Year { get; set; }
}
}
You can use Workflow Foundation to evaluate expressions, but it is far easier to use almost any other option.
The key issue at play with your code was that you were not trying to evaluate the expression (with either VisualBasicValue or CSharpValue). Assigning InArgument`1.Expression is an attempt to set the value - not to set the value to the result of an expression.
Keep in mind that compiling expressions is fairly slow (>10ms), but the resultant compiled expression can be cached for quick executions.
Using Workflow:
class Program
{
static void Main(string[] args)
{
// this is slow, only do this once per expression
var evaluator = new PolicyExpressionEvaluator("Policy.Premium * 1.05");
// this is fairly fast
var policy1 = new Policy() { Premium = 100, Year = 2016 };
var result1 = evaluator.Evaluate(policy1);
var policy2 = new Policy() { Premium = 150, Year = 2016 };
var result2 = evaluator.Evaluate(policy2);
Console.WriteLine($"Policy 1: {result1}, Policy 2: {result2}");
}
}
public class Policy
{
public double Premium, Year;
}
class PolicyExpressionEvaluator
{
const string
ParamName = "Policy",
ResultName = "result";
public PolicyExpressionEvaluator(string expression)
{
var paramVariable = new Variable<Policy>(ParamName);
var resultVariable = new Variable<double>(ResultName);
var daRoot = new DynamicActivity()
{
Name = "DemoExpressionActivity",
Properties =
{
new DynamicActivityProperty() { Name = ParamName, Type = typeof(InArgument<Policy>) },
new DynamicActivityProperty() { Name = ResultName, Type = typeof(OutArgument<double>) }
},
Implementation = () => new Assign<double>()
{
To = new ArgumentReference<double>() { ArgumentName = ResultName },
Value = new InArgument<double>(new CSharpValue<double>(expression))
}
};
CSharpExpressionTools.CompileExpressions(daRoot, typeof(Policy).Assembly);
this.Activity = daRoot;
}
public DynamicActivity Activity { get; }
public double Evaluate(Policy p)
{
var results = WorkflowInvoker.Invoke(this.Activity,
new Dictionary<string, object>() { { ParamName, p } });
return (double)results[ResultName];
}
}
internal static class CSharpExpressionTools
{
public static void CompileExpressions(DynamicActivity dynamicActivity, params Assembly[] references)
{
// See https://learn.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/csharp-expressions
string activityName = dynamicActivity.Name;
string activityType = activityName.Split('.').Last() + "_CompiledExpressionRoot";
string activityNamespace = string.Join(".", activityName.Split('.').Reverse().Skip(1).Reverse());
TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
{
Activity = dynamicActivity,
Language = "C#",
ActivityName = activityType,
ActivityNamespace = activityNamespace,
RootNamespace = null,
GenerateAsPartialClass = false,
AlwaysGenerateSource = true,
ForImplementation = true
};
// add assembly references
TextExpression.SetReferencesForImplementation(dynamicActivity, references.Select(a => (AssemblyReference)a).ToList());
// Compile the C# expression.
var results = new TextExpressionCompiler(settings).Compile();
if (results.HasErrors)
{
throw new Exception("Compilation failed.");
}
// attach compilation result to live activity
var compiledExpression = (ICompiledExpressionRoot)Activator.CreateInstance(results.ResultType, new object[] { dynamicActivity });
CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(dynamicActivity, compiledExpression);
}
}
Compare to the equivalent Roslyn code - most of which is fluff that is not really needed:
public class PolicyEvaluatorGlobals
{
public Policy Policy { get; }
public PolicyEvaluatorGlobals(Policy p)
{
this.Policy = p;
}
}
internal class PolicyExpressionEvaluator
{
private readonly ScriptRunner<double> EvaluateInternal;
public PolicyExpressionEvaluator(string expression)
{
var usings = new[]
{
"System",
"System.Collections.Generic",
"System.Linq",
"System.Threading.Tasks"
};
var references = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => !a.IsDynamic && !string.IsNullOrWhiteSpace(a.Location))
.ToArray();
var options = ScriptOptions.Default
.AddImports(usings)
.AddReferences(references);
this.EvaluateInternal = CSharpScript.Create<double>(expression, options, globalsType: typeof(PolicyEvaluatorGlobals))
.CreateDelegate();
}
internal double Evaluate(Policy policy)
{
return EvaluateInternal(new PolicyEvaluatorGlobals(policy)).Result;
}
}
Roslyn is fully documented, and has the helpful Scripting API Samples page with examples.
I have the following three methods in the CompanyApplication class (along with the supporting factories and services listed):
public ResultSet<CompanyDto> AddCompany(CompanyDto companyDto)
{
var result = new CompanyDto();
var company = new Company();
Mapper.Map(companyDto, company);
using (ITransaction t = _transactionFactory.Create())
{
company = _companyService.Add(company);
t.Commit();
}
Mapper.Map(company, result);
return new ResultSet<CompanyDto>(1, new[] { result });
}
public ResultSet<CompanyContactDto> AddCompanyContact(CompanyContactDto companyContactDto)
{
var result = new CompanyContactDto();
var company = new Company();
var contact = new CompanyContact();
Mapper.Map(companyContactDto, contact);
using (ITransaction t = _transactionFactory.Create())
{
var contactCompanies = FindByIdJoin<Company, CompanyDto>(companyContactDto.CompanySK);
Mapper.Map(contactCompanies.Data.First(), company);
company.CompanyContacts.Add(contact);
company = _companyService.Update(company);
t.Commit();
}
Mapper.Map(contact, result);
return new ResultSet<CompanyContactDto>(1, new[] { result });
}
public ResultSet<T_DtoType> FindByIdJoin<T_DbType, T_DtoType>(long id)
{
IAbstractRepository<T_DbType> repository = EnsureRepository<T_DbType>();
T_DbType entity = repository.FindByIdJoin(id);
return (entity == null ? null : MapResultSetToDto<T_DbType, T_DtoType>(entity));
}
There are other objects in play here, which is why the FindByIdJoin has been made a separate method in the CompanyApplication class.
I have set up the testing class with some mocks and an instance of the CompanyApplication class:
private Mock<ICompanyRepository> _mockCompanyRepository;
private Mock<ICompanyDomain> _mockCompanyService;
private Mock<ITransactionFactory> _mockTransactionFactory;
private Mock<ITransaction> _mockTransaction;
private CompanyApplication _companyApplication;
[Setup]
public void SetUp()
{
_mockCompanyRepository = new Mock<ICompanyRepository>(MockBehavior.Strict);
_mockCompanyService = new Mock<ICompanyDomain>(MockBehavior.Strict);
_mockTransactionFactory = new Mock<ITransactionFactory>(MockBehavior.Strict);
_mockTransaction = new Mock<ITransaction>(MockBehavior.Strict);
_companyApplication = new CompanyApplication(
_mockCompanyRepository.Object,
_mockCompanyService.Object,
_mockTransactionFactory.Object);
}
I am successfully able to test the FindByIdJoin and AddCompany methods directly in Moq like this:
[Test]
public void CanFindCompanyByIdJoin()
{
var data = new Company {ObjectId = 1, Name = "Company1"};
_mockCompanyRepository.Setup(x => x.FindByIdJoin(It.Is<long>(arg => arg == data.ObjectId)))
.Returns(data);
var result = _companyApplication.FindByIdJoin<Company, CompanyDto>(data.ObjectId);
Assert.AreEqual(data.ObjectId, result.Data.First().ObjectId);
}
[Test]
public void CanAddCompany()
{
var data = new Company {ObjectId = 1, Name = "Company1"};
_mockCompanyService.Setup(x => x.Add(It.Is<Company>(arg => arg.ObjectId == data.ObjectId)))
.Returns(data);
_mockTransactionFactory.Setup(x => x.Create()).Returns(_mockTransaction.Object);
_mockTransaction.Setup(x => x.Commit());
_mockTransaction.Setup(x => x.Dispose());
var dto = new CompanyDto {ObjectId = 1, Name = "Company1"};
var result = _companyApplication.AddCompany(dto);
_mockCompanyService.Verify(t => t.Add(It.IsAny<Company>()));
}
Those two tests pass just fine. However, I'm having trouble coming up with a test for AddCompanyContact, because it calls FindByIdJoin as part of its flow, and that seems to be getting in the way.
Specifically, is there a way to mock var contactCompanies = FindByIdJoin<Company, CompanyDto>(companyContactDto.CompanySK) in a test for the AddCompanyContact method?
Thanks!
There is two alternatives that i see depending on the amount of work that you want to do.
Wrap that call into a object and instantiate it using a IOC container. This is the one that i feel would take the most effort if you are not using one already.
Turn that call into a Func and make a method without that parameter that does the call. This approach has the disadvantage that the top call will be untestable but will allow access to the rest of the method.
Example Below:
public ResultSet<CompanyContactDto> AddCompanyContact(CompanyContactDto companyContactDto)
{
AddCompanyContact(CompanyContactDto, ()=>
{
return FindByIdJoin<Company, CompanyDto> companyContactDto.CompanySK);
}
}
public ResultSet<CompanyContactDto> AddCompanyContact(CompanyContactDto companyContactDto, Func<WhateverTheMethodReturns> findIdReplacement)
{
var result = new CompanyContactDto();
var company = new Company();
var contact = new CompanyContact();
Mapper.Map(companyContactDto, contact);
using (ITransaction t = _transactionFactory.Create())
{
var contactCompanies = findIdReplacement();
Mapper.Map(contactCompanies.Data.First(), company);
company.CompanyContacts.Add(contact);
company = _companyService.Update(company);
t.Commit();
}
Mapper.Map(contact, result);
return new ResultSet<CompanyContactDto>(1, new[] { result });
}
I was over complicating the problem... since AddCompanyContact calls FindByIdJoin, all I needed to do was mock the same interface that is used in FindByIdJoin.
Lesson learned: mock interfaces, not classes.