Skip to content
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

Is there a way to assert "cookie existance"? #265

Closed
M-Adam opened this Issue Dec 9, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@M-Adam
Copy link

M-Adam commented Dec 9, 2016

In my app I use cookies to display simple success/error messages. I do it like that:

protected void ViewError(Exception e)
{
   HttpContext.Response.Cookies.Append("ErrorMessageKey", e.GetExceptionMessage());
}

and then I delete them in client-side javascript after showing it to the User.

Is there a way to check if a cookie with a specified key was set in action? Or if cookie exists?
I was thinkink about something like this:

//calling Register() action will set ErrorMessage in cookies

MyMvc.Controller<AccountController>()
                .WithDbContext(builder => builder.WithSet<User>(set => set.Add(new User() {Username = "foo", Password = "foo", Email = "foo"})))
                .Calling(x => x.Register(registerViewModel))
                .ShouldReturn()
                .View()
                .WithModel(registerViewModel)
                .AndAlso()
                .ShouldPassForThe<HttpContext>(x=>x.Response.Cookies.Exists("ErrorMessageKey"));

Anyway, amazing library, makes you want to do TDD, thanks for sharing! 👍

@ivaylokenov

This comment has been minimized.

Copy link
Owner

ivaylokenov commented Dec 10, 2016

@M-Adam You can use the built-in HttpResponse assertion:

MyMvc.Controller<AccountController>()
                .WithDbContext(builder => builder.WithSet<User>(set => set.Add(new User() {Username = "foo", Password = "foo", Email = "foo"})))
                .Calling(x => x.Register(registerViewModel))
                .ShouldHave()
                .HttpResponse(response => response
                        .ContainingCookie("ErrorMessageKey"))
                .AndAlso()
                .ShouldReturn()
                .View()
                .WithModel(registerViewModel);

Let me know if the above example does what you need. 👍

P. S. May I add this line: amazing library, makes you want to do TDD as a testimonial on the official web-site - https://mytestedasp.net ?

@ivaylokenov ivaylokenov added this to the 1.1.0 milestone Dec 10, 2016

@ivaylokenov ivaylokenov self-assigned this Dec 10, 2016

@M-Adam

This comment has been minimized.

Copy link
Author

M-Adam commented Dec 10, 2016

Thanks, it works. I must have overlooked that method.

Of course, feel free to use it :)

@ivaylokenov

This comment has been minimized.

Copy link
Owner

ivaylokenov commented Dec 10, 2016

@M-Adam Thank you. Your job title is "software developer", I guess?

I believe you can close the issue now.

@M-Adam M-Adam closed this Dec 10, 2016

@ivaylokenov ivaylokenov added the done label Feb 25, 2017

@ivaylokenov ivaylokenov removed done labels Dec 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.