public void Executed_logs_every_command_when_context_not_set() { var context1 = new Mock <DbContext>().Object; var context2 = new Mock <DbContext>().Object; var writer = new StringWriter(); var formatter = new DatabaseLogFormatter(writer.Write); formatter.Executed(CreateCommand(""), new DbCommandInterceptionContext <string> { Result = "Would you like them" }); var interceptionContext = new DbCommandInterceptionContext <string>().WithDbContext(context1); interceptionContext.Result = "Here or there?"; formatter.Executed(CreateCommand(""), interceptionContext); interceptionContext = new DbCommandInterceptionContext <string>().WithDbContext(context2); interceptionContext.Result = "I would not like them"; formatter.Executed(CreateCommand(""), interceptionContext); var lines = GetLines(writer); Assert.Equal(Strings.CommandLogComplete(0, "Would you like them", ""), lines[0]); Assert.Equal(Strings.CommandLogComplete(0, "Here or there?", ""), lines[2]); Assert.Equal(Strings.CommandLogComplete(0, "I would not like them", ""), lines[4]); }
public void Executed_validates_arguments() { var formatter = new DatabaseLogFormatter(new StringWriter().Write); Assert.Equal( "command", Assert.Throws <ArgumentNullException>(() => formatter.Executed(null, new DbCommandInterceptionContext <int>())) .ParamName); Assert.Equal( "interceptionContext", Assert.Throws <ArgumentNullException>(() => formatter.Executed <int>(new Mock <DbCommand>().Object, null)).ParamName); }
public void Executed_filters_by_context_when_set() { var context1 = new Mock <DbContext>().Object; var context2 = new Mock <DbContext>().Object; var writer = new StringWriter(); var formatter = new DatabaseLogFormatter(writer.Write); var interceptionContext = new DbCommandInterceptionContext <string>().WithDbContext(context1); interceptionContext.Result = "Sam-I-am"; formatter.Executed(CreateCommand(""), interceptionContext); interceptionContext = new DbCommandInterceptionContext <string>().WithDbContext(context2); interceptionContext.Result = "I do not like"; formatter.Executed(CreateCommand(""), interceptionContext); formatter.Executed(CreateCommand(""), new DbCommandInterceptionContext <string> { Result = "Green eggs and ham" }); Assert.Equal(Strings.CommandLogComplete(0, "Sam-I-am", ""), GetSingleLine(writer)); }