Skip to content

Commit

Permalink
ShouldThrowValidationExceptionIfInvalidParametersOnGetServiceWithSpal…
Browse files Browse the repository at this point in the history
… -> FAIL
  • Loading branch information
LBoullosa committed Oct 6, 2024
1 parent c7a22a2 commit ee3ef77
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,48 @@ private void ShouldThrowValidationExceptionIfInvalidParametersOnGetService(

this.dependencyInjectionBroker.VerifyNoOtherCalls();
}

[Theory]
[MemberData(nameof(GetServiceWithSpalValidationExceptions))]
private void ShouldThrowValidationExceptionIfInvalidParametersOnGetServiceWithSpal(
DependencyInjection someDependencyInjection,
string someSpalId,
Xeption exception)
{
// given
var expectedDependencyInjectionValidationException =
new DependencyInjectionValidationException(
message: "Dependency Injection validation error occurred, fix errors and try again.",
innerException: exception);

this.dependencyInjectionBroker
.Setup(broker =>
broker.GetService<ISPALBase>(
It.IsAny<IServiceProvider>(),
It.IsAny<string>()));

// when
Func<ISPALBase> getServiceFunction = () =>
this.dependencyInjectionService.GetService<ISPALBase>(
someDependencyInjection,
someSpalId);

DependencyInjectionValidationException actualDependencyInjectionValidationException =
Assert.Throws<DependencyInjectionValidationException>(
getServiceFunction);

//then
actualDependencyInjectionValidationException.Should().BeEquivalentTo(
expectedDependencyInjectionValidationException);

this.dependencyInjectionBroker
.Verify(broker =>
broker.GetService<ISPALBase>(
It.IsAny<IServiceProvider>(),
It.IsAny<string>()),
Times.Never);

this.dependencyInjectionBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -592,5 +592,45 @@ public static TheoryData<DependencyInjection, Xeption> GetServiceValidationExcep
},
};
}

public static TheoryData<DependencyInjection, string, Xeption> GetServiceWithSpalValidationExceptions()
{
return new TheoryData<DependencyInjection, string, Xeption>
{
{
null,
null,
CreateInvalidDependencyInjectionParameterException(
new Dictionary<string, string>
{
{nameof(DependencyInjection), "object is required" }
})
},

{
new DependencyInjection(),
null,
CreateInvalidServiceProviderParameterException(
new Dictionary<string, string>
{
{nameof(ServiceProvider), "object is required" }
})
},

{
new DependencyInjection
{
ServiceCollection = new ServiceCollection(),
ServiceProvider = new ServiceCollection().BuildServiceProvider()
},
null,
CreateInvalidServiceProviderParameterException(
new Dictionary<string, string>
{
{"spalI", "Value is required" }
})
},
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,11 @@ public T GetService<T>(DependencyInjection dependencyInjection) =>
dependencyInjection.ServiceProvider);
});

public T GetService<T>(DependencyInjection dependencyInjection, string spalId) =>
TryCatchGetService(() =>
public T GetService<T>(DependencyInjection dependencyInjection, string spalId)
{
ValidateServiceProvider(dependencyInjection);

return dependencyInjectionBroker.GetService<T>(
dependencyInjection.ServiceProvider,
spalId);
});
}
}
}

0 comments on commit ee3ef77

Please sign in to comment.