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

Error using HttpTest with MSTest #207

Closed
hrkeni opened this issue Aug 29, 2017 · 16 comments
Closed

Error using HttpTest with MSTest #207

hrkeni opened this issue Aug 29, 2017 · 16 comments

Comments

@hrkeni
Copy link

hrkeni commented Aug 29, 2017

I'm trying to use Flurl.Http.Testing.HttpTest for some unit tests. Any time I try to run these tests I get the follow error:
An exception occurred while invoking executor 'executor://mstestadapter/v1': Type 'Flurl.Http.Testing.HttpTest' in assembly 'Flurl.Http, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Any suggestions?

@tmenier
Copy link
Owner

tmenier commented Aug 29, 2017

Can you post the code for the test?

@hrkeni
Copy link
Author

hrkeni commented Aug 30, 2017

I believe this code isn't even getting a chance to execute because the test runner is having issues with HttpTest. Just in case, this is how I am using the HttpTest object:

        [TestInitialize]
        public void CreateHttpTest()
        {
            _httpTest = new HttpTest();
            <other init code>
        }

And using it in a test as such:

[TestMethod]
        public async Task TesMyAction()
        {
            var responseObj = new
            {
                test = "123"
            };
            var serialized = JsonConvert.SerializeObject(responseObj);
            var uri = new Uri("http://test.endpoint.com/test");
            _httpTest.RespondWith(new StringContent(serialized));

            var controller = new MyController();
            <other code>
            HttpRequestMessage fakeRequest = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = uri
            };

            controller.Request = fakeRequest;

            HttpResponseMessage received = controller.Respond("api/test").Result;
            Assert.AreEqual(serialized, received.Content.ReadAsStringAsync().Result);
            Assert.AreEqual(JsonConvert.SerializeObject(ctx),
                _httpTest.CallLog.FirstOrDefault().Request.Headers.GetValues("X-Application-Context").FirstOrDefault());
        }

@tmenier
Copy link
Owner

tmenier commented Sep 3, 2017

Have you tried a different test runner? I've never seen this so I suspect it's specific to MSTest. I can look into it but not until after the Flurl.Http 2.0 release.

@tmenier
Copy link
Owner

tmenier commented Nov 28, 2017

Do you have a stack trace? Without that I can't be of much help. It it seems that the MSTest runtime wants to serialize HttpTest for some reason. If using a different test runner isn't feasible, try dropping the _httpTest field and instead create HttpTest instances as needed inside using statements in your tests.

If that still doesn't work, try posting the question on Stack Overflow and maybe an MSTest expert will chime in. I'm using NUnit and ReSharper and I haven't encountered anything like this.

@tmenier
Copy link
Owner

tmenier commented Dec 9, 2017

Closing this issue. If you still need help, please ask on Stack Overflow.

@tmenier tmenier closed this as completed Dec 9, 2017
@kenoma
Copy link

kenoma commented Dec 12, 2017

I have same issue with Nunit
System.Runtime.Serialization.SerializationException : Type 'Flurl.Http.Testing.HttpTest' in assembly 'Flurl.Http, Version=2.1.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

@ZombiesWithCoffee
Copy link

Just in case someone else hits this, I have the same issue. This is how we should be initializing HttpTest.

    [ClassInitialize]
    public static void Initialize(TestContext testContext)
    {
        _api = new MyApi();

        _httpTest = new HttpTest();
        _httpTest.RespondWith(File.ReadAllText("./Data/myData.json"));
    }

    static PoloniexApi _api;
    static HttpTest _httpTest;

But since HttpTest doesn't have the [Serializable] attribute on the class, it's causing MSTest to fail.

One solution is to move the HttpTest into [TestInitialize] and [TestCleanup] instead. Then MSTest won't have to try to serialize HttpTest between test cases.

    [TestInitialize]
    public void TestInitialize()
    {
        _httpTest = new HttpTest();
        _httpTest.RespondWith(File.ReadAllText("./Data/myData.json"));
    }

    HttpTest _httpTest;

    [TestCleanup]
    public void TestCleanup()
    {
        _httpTest.Dispose();
    }

@tmenier
Copy link
Owner

tmenier commented Feb 18, 2018

@ZombiesWithCoffee @hrkeni @kenoma I'm willing to re-open this issue if any of you would be willing to help test it. Taking a fresh look at it, it's quite possible that all I need to do is add [ISerializable] to HttpTest. Everything builds and tests pass when I do that, but I've never encountered the problem myself.

Here's what I'm asking for help with:

  1. Verify that you can reproduce this problem with the current version of Flurl.Http.
  2. Get the 2.2 prerelease (which I hope to have ready today) and verify that the problem is fixed.

This would be tremendously helpful. Volunteers?

@tmenier tmenier reopened this Feb 18, 2018
@tmenier tmenier changed the title Can't run tests when using HttpTest Error using HttpTest with MSTest Feb 18, 2018
@ZombiesWithCoffee
Copy link

Thanks @tmenier, but I'm going to say that it would be better to leave it as is. Since _httpTest.RespondWith only affects the next Flurl call, allowing HttpTest in the [ClassInitialize]/[ClassCleanup] would cause any secondary test to fail.

The proper location for this is [TestInitialize]/[TestCleanup]

@ZombiesWithCoffee
Copy link

This is how I'm now structuring my tests in MSTest/Visual Studio 2017

[TestClass]
public class MyTests
{
    #region API Initialize

    static MyApi _api;

    [ClassInitialize]
    public static void Initialize(TestContext testContext)
    {
        _api = new MyApi ();
    }

    [ClassCleanup]
    public static void Cleanup()
    {
        _api = null;
    }

    #endregion
    #region Test Initialize

    HttpTest _httpTest;

    [TestInitialize]
    public void TestInitialize()
    {
        _httpTest = new HttpTest();
        _httpTest.RespondWith(File.ReadAllText("./Data/mydata.json"));
    }

    [TestCleanup]
    public void TestCleanup()
    {
        _httpTest.Dispose();
    }

    #endregion

    [TestMethod]
    public async Task MyFirstTest()
    {
        var datum= await _api.GetTestAsync();

        Assert.AreEqual(90, datum.Count());
    }

    [TestMethod]
    public async Task MySecondTest()
    {
        var datum= await _api.GetTestAsync();
        ...
    }

@tmenier tmenier added this to the Flurl.Http 2.2 milestone Feb 18, 2018
tmenier added a commit that referenced this issue Feb 18, 2018
@tmenier
Copy link
Owner

tmenier commented Feb 19, 2018

@ZombiesWithCoffee I agree completely, but who's using [ClassInitialize]? This bug, as reported, involves [TestInitialize].

Can anyone repro this with [TestInitialize]? If so, I'd still ask you to report whether the latest prerelease fixes it, which is now available:

https://www.nuget.org/packages/Flurl.Http/2.2.0-pre1

@ZombiesWithCoffee
Copy link

Well, don't I feel stupid? But at least I can replicate his error. I'd be happy to test it out for you.

@ZombiesWithCoffee
Copy link

I'm not getting anything in the link
https://www.nuget.org/packages/Flurl.Http/2.2.0-pre1

@tmenier
Copy link
Owner

tmenier commented Feb 19, 2018

LOL, no problem. Looks like the link doesn't work publicly yet because the package is still in "validating" status. Been that way for 3+ hours. I contacted support, will update here when it's available.

Thanks for your help!

@tmenier
Copy link
Owner

tmenier commented Feb 19, 2018

@ZombiesWithCoffee The prerelease is available, finally.

@tmenier
Copy link
Owner

tmenier commented Feb 22, 2018

Full version is released. Please re-open if this issue is not resolved.

@tmenier tmenier closed this as completed Feb 22, 2018
@tmenier tmenier removed the testing label Feb 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants