Skip to content

Commit

Permalink
Merge pull request #258 from Pressacco/Features/257-LogPreInteraction…
Browse files Browse the repository at this point in the history
…AsTrace

Merge branch to `master` for: logging `attempts to` and `asks for` as trace level log messages
  • Loading branch information
AutomationPanda authored May 25, 2023
2 parents 7134d48 + cc2cf8e commit b59d2b3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,14 @@ public void AsksFor()
[Test]
public void ActorNameLoggedForAnswer()
{
var responseMessage = string.Empty;
var hasAskedQuestion = false;
var logMessage = string.Empty;

var MockLogger = new Mock<ILogger>();

MockLogger.Setup(x => x.Info(It.IsAny<string>()))
.Callback<string>(message =>
{
if (hasAskedQuestion)
{
responseMessage = message;
}
else
{
hasAskedQuestion = true;
}
logMessage = message;
});

var MockQuestion = new Mock<IQuestion<bool>>();
Expand All @@ -116,28 +108,20 @@ public void ActorNameLoggedForAnswer()
var joe = new Actor("Joe", MockLogger.Object);
joe.AsksFor(MockQuestion.Object);

responseMessage.Should().StartWith("Screenplay Actor 'Joe' observed that the");
logMessage.Should().StartWith("Screenplay Actor 'Joe' observed that the");
}

[Test]
public async Task ActorNameLoggedForAnswerAsync()
{
var responseMessage = string.Empty;
var hasAskedQuestion = false;
var logMessage = string.Empty;

var MockLogger = new Mock<ILogger>();

MockLogger.Setup(x => x.Info(It.IsAny<string>()))
.Callback<string>(message =>
{
if (hasAskedQuestion)
{
responseMessage = message;
}
else
{
hasAskedQuestion = true;
}
logMessage = message;
});

var MockQuestion = new Mock<IQuestionAsync<bool>>();
Expand All @@ -147,7 +131,67 @@ public async Task ActorNameLoggedForAnswerAsync()
var joe = new Actor("Joe", MockLogger.Object);
_ = await joe.AsksForAsync(MockQuestion.Object);

responseMessage.Should().StartWith("Screenplay Actor 'Joe' observed that the");
logMessage.Should().StartWith("Screenplay Actor 'Joe' observed that the");
}

[Test]
public void TaskHasCorrectLogSeverity()
{
string preTaskMessage = string.Empty;
string postTaskMessage = string.Empty;

Mock<ILogger> MockLogger = new Mock<ILogger>();

MockLogger.Setup(x => x.Trace(It.IsAny<string>()))
.Callback<string>(message =>
{
preTaskMessage = message;
});

MockLogger.Setup(x => x.Info(It.IsAny<string>()))
.Callback<string>(message =>
{
postTaskMessage = message;
});

Mock<ITask> MockQuestion = new Mock<ITask>();
MockQuestion.Setup(x => x.ToString()).Returns("'OkButton' Enabled");

Actor joe = new Actor("Joe", MockLogger.Object);
joe.AttemptsTo(MockQuestion.Object);

preTaskMessage.Should().StartWith("Screenplay Actor 'Joe' attempts to");
postTaskMessage.Should().StartWith("Screenplay Actor 'Joe' successfully did");
}

[Test]
public void QuestionHasCorrectLogSeverity()
{
string preQuestionMessage = string.Empty;
string postQuestionMessage = string.Empty;

Mock<ILogger> MockLogger = new Mock<ILogger>();

MockLogger.Setup(x => x.Trace(It.IsAny<string>()))
.Callback<string>(message =>
{
preQuestionMessage = message;
});

MockLogger.Setup(x => x.Info(It.IsAny<string>()))
.Callback<string>(message =>
{
postQuestionMessage = message;
});

Mock<IQuestion<bool>> MockQuestion = new Mock<IQuestion<bool>>();
MockQuestion.Setup(x => x.ToString()).Returns("'OkButton' Enabled");

Actor joe = new Actor("Joe", MockLogger.Object);
joe.AsksFor(MockQuestion.Object);

preQuestionMessage.Should().StartWith("Screenplay Actor 'Joe' asks for");
postQuestionMessage.Should().StartWith("Screenplay Actor 'Joe' observed that");
}

[Test]
Expand Down
1 change: 1 addition & 0 deletions Boa.Constrictor.Screenplay/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Log entry now includes the following prefix when an `Interaction` answer is received:
- `{ActorName} observed that the`
- Pre-_interaction_ messages (e.g.`attempts to` and `asks for`) are now logged as `Trace` to facilitate log file filtering.


## [3.0.3] - 2022-12-13
Expand Down
8 changes: 4 additions & 4 deletions Boa.Constrictor.Screenplay/Screenplay/Pattern/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Actor(string name = null, ILogger logger = null)
/// <returns></returns>
private TAnswer CallQuestion<TAnswer>(IQuestion<TAnswer> question, string observationPhrase, string enterPhrase, string exitPhrase)
{
Logger.Info($"{this} {enterPhrase} {question}");
Logger.Trace($"{this} {enterPhrase} {question}");
TAnswer answer = question.RequestAs(this);
Logger.Info($"{this} {observationPhrase} {question} {exitPhrase} {answer}");
return answer;
Expand All @@ -86,7 +86,7 @@ private TAnswer CallQuestion<TAnswer>(IQuestion<TAnswer> question, string observ
/// <returns></returns>
private async Task<TAnswer> CallQuestionAsync<TAnswer>(IQuestionAsync<TAnswer> question, string observationPhrase, string enterPhrase, string exitPhrase)
{
Logger.Info($"{this} {enterPhrase} {question}");
Logger.Trace($"{this} {enterPhrase} {question}");
TAnswer answer = await question.RequestAsAsync(this);
Logger.Info($"{this} {observationPhrase} {question} {exitPhrase} {answer}");
return answer;
Expand All @@ -100,7 +100,7 @@ private async Task<TAnswer> CallQuestionAsync<TAnswer>(IQuestionAsync<TAnswer> q
/// <param name="exitPhrase">The phrase to print after calling.</param>
public void CallTask(ITask task, string enterPhrase, string exitPhrase)
{
Logger.Info($"{this} {enterPhrase} {task}");
Logger.Trace($"{this} {enterPhrase} {task}");
task.PerformAs(this);
Logger.Info($"{this} {exitPhrase} {task}");
}
Expand All @@ -113,7 +113,7 @@ public void CallTask(ITask task, string enterPhrase, string exitPhrase)
/// <param name="exitPhrase">The phrase to print after calling.</param>
public async Task CallTaskAsync(ITaskAsync task, string enterPhrase, string exitPhrase)
{
Logger.Info($"{this} {enterPhrase} {task}");
Logger.Trace($"{this} {enterPhrase} {task}");
await task.PerformAsAsync(this);
Logger.Info($"{this} {exitPhrase} {task}");
}
Expand Down

0 comments on commit b59d2b3

Please sign in to comment.