-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TempData & ViewBag Support #27
Comments
In terms of adding ViewBag support and an idea of a syntax for it check out: #4 In terms of a syntax for Maybe something like (?): _controller.ShouldHaveTempDataProperty<string>("key")
_controller.ShouldHaveTempDataProperty<string>("key", "value")
_controller.ShouldHaveTempDataProperty<string>("key", s => s.EndsWith("ue"))
_controller.ShouldHaveTempDataProperty<string>("key", s => { Assert.That(s, Is.EqualTo("value"); }) |
I strongly recalled reading an issue about view bag support but when I skimmed both the open and closed issues I could not see it - I thought I must be mad! Turns out it was a pull request ugh. Oh wow. That syntax looks superb Rob. Unless somebody else wants to take this on, I will work on this soon. |
Ha! Happens to the best of us :P I suspect nobody is on this so feel free :) |
It occurred to me that we could enable syntax like this: [Test]
public void Delete_ExistentPost_ReturnsMessage()
{
// Regular method on ControllerResultTest<T>.
controller
.WithCallTo(c => c.Delete(""))
.ShouldHaveTempDataProperty<string>("Message")
} This is different from your proposed solution: [Test]
public void Delete_ExistentPost_ReturnsMessage()
{
controller.Delete("");
// Extension method on Controller.
controller
.ShouldHaveTempDataProperty<string>("Message");
} The benefit of this proposed solution is consistency with the rest of the API. The drawback however, is that when you want to perform multiple logical assertions, the syntax is less than nice: [Test]
public void Delete_ExistentPost_RedirectsToIndexWithMessage()
{
controller
.WithCallTo(c => c.Delete(""))
.ShouldRedirectTo(c => c.Index);
// Regular method on ControllerResultTest<T>. Eww.
controller
.WithCallTo(c => c.Delete(""))
.ShouldHaveTempDataProperty<string>("Message")
} This is different from your proposed approach: [Ignore]
public void Delete_ExistentPost_RedirectsToIndexWithMessage()
{
controller
.WithCallTo(c => c.Delete(""))
.ShouldRedirectTo(c => c.Index);
// Extension method on Controller.
controller
.ShouldHaveTempDataProperty<string>("Message");
} I am on the fence about which approach to take. Which do you prefer? |
I prefer what I suggested just because it means we aren't needing to invoke the method twice. Thoughts?
|
I was leaning that way too so yeah, all good. I will start on this now. |
How do you want to handle multiple tests? _controller.ShouldHaveTempDataProperty<string>("a")
_controller.ShouldHaveTempDataProperty<string>("b") _controller
.ShouldHaveTempDataProperty<string>("a")
.ShouldHaveTempDataProperty<string>("b") _controller
.ShouldHaveTempDataProperty<string>("a")
.AndShouldHaveTempDataProperty<string>("b") |
Probably with an action
.ShouldThrow<RuleViolationException>()
.WithMessage("change the unit of an existing ingredient", ComparisonMode.Substring)
.And.Violations.Should().Contain(BusinessRule.CannotChangeIngredientQuanity); So your example above would be: _controller
.ShouldHaveTempDataProperty<string>("a")
.And.ShouldHaveTempDataProperty<string>("b") |
That seems reasonable.
|
I do not understand the purpose of the Especially because elsewhere in the library we do this: controller
.WithCallTo(c => c.Index())
.ShouldRenderDefaultView()
.WithModel<string>()
.AndModelError("foo")
.AndModelError("bar"); |
Maybe just make the new extension return the controller and leave it at that?
|
That would conform to the syntax I described earlier: _controller
.ShouldHaveTempDataProperty<string>("a")
.ShouldHaveTempDataProperty<string>("b") Especially easy to implement but not very fluent, wouldn't you say? |
You could have an AndShouldHaveTempDataProperty method that is a synonym too?
|
That would enable syntax like this though, would it not?: _controller
.AndShouldHaveTempDataProperty<string>("a")
.ShouldHaveTempDataProperty<string>("b") We could perhaps return a |
Yep that works too :) |
I don't have much experience with this library, so I'm not qualified to comment too much on the API. I'm sure you both will come up with something great. I think that the
If you take @byteblast 's ShouldHaveTempDataProperty and adapt it to return an AndConstraint rather than void (or presumably Controller eventually).
then this syntax becomes possible, which I think is quite "fluent".
This approach could potentially be used elsewhere in the API - breaking changes of course! :-) Here, I've added a
Which would allow these use cases:
I think this avoids the issue @byteblast raised with having And methods before Should methods, which I agree is not ideal. You would only ever get And after your first call to a method, as it is being returned from that method. The use of an And constraint shouldn't imply that there could also be an Or constraint. It is really syntactic sugar to chain multiple assertions and there is no And/Or logic going on between assertions. Thoughts? |
While the code behind it is more complex, I actually prefer having less |
Yes, that is similar to Shouldly vs Fluent Assertions, and it's a personal preference thing. I think the current API is good the way it is, from what I've seen. This approach starts to have advantages if you were to introduce the AndShouldHaveTempDataProperty suggested above, where you get into the odd situation of having the And method before the Should method, as it constrains the order. Perhaps then, if you just stick to the one ShouldHaveTempDataProperty that would be consistent with the multiple calls to AndModelError style of the current API listed above. |
@mwhelan, I think you make a good argument but in the end I have to agree with @robdmoore - less |
No worries :-) |
Earlier today I had to write a unit test against the
TempData
property and wondered if there was any support in this library for this - there is not.The problem that I have is that
TempData
is basically weakly typed and casting is not elegant_. I love the way this library enables strong typing when possible and thought It *might_ be awesome if there was some support for this.Because there would already be support for
TempData
I guess it would make sense to have aViewBag
counterpart too.I am not entirely convinced as to how valuable this feature would be nor am I confident about what the syntax might look like, but I thought I would throw it out there for discussion anyway.
*It is not so much that I find casting inelegant but rather - I use this library for all my controller tests and I find that altering between this library and standard tests does not look fluent and consequently, neat.
The text was updated successfully, but these errors were encountered: