This known pattern for BeforeAfterTestAttribute works as intended but i'm trying to do a bit extra on the after method. I'm trying to pass the value of a member which has just been "acted" in ProfileTests class, named json
//[WriteFileAfter("Company.json", nameof(json))] //Not working
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class WriteFileAfter : BeforeAfterTestAttribute {
public string FileName { get; }
public string Json { get; }
public WriteFileAfter(string fileName, string json) {
FileName = fileName;
Json = json;
}
//public override void Before(MethodInfo methodUnderTest) { //ignore
// Debug.WriteLine(methodUnderTest.Name); }
public override void After(MethodInfo methodUnderTest) {
Debug.WriteLine(methodUnderTest.Name);
JsonUtils.WriteJsonToFile(**Json**, methodUnderTest.Name);
//^-- Json = 'json'
}
}
The following code shows me using this attribute and passing "json" which is the nameof json, yes I understand it's just 'json'
public class ProfileTests
{
public string json;
public ProfileTests()
{
}
[Theory]
[InlineData(@"test.txt")]
[WriteFileAfter("Company.json", nameof(json))]
public void MyTest(string fileName) {
// Arrange
// Act
// Assert
//Output?
//json = JsonConvert.SerializeObject(package);
json = "Hello World"
}
}
How do I reflect back to the "value" of json so I can pass it to JsonUtils.WriteJsonToFile method because I know the name of the attribute I want the value. I've tried various reflection hacks (no expert) and can't seem to grasp how to do this. Thanks.
nameof(json) will return 'json' as expected but I wan the value I've google a bit but can't figure out the correct syntax.
Aucun commentaire:
Enregistrer un commentaire