{"action":"created","issue":{"url":"https://api.github.com/repos/xunit/xunit/issues/434","labels_url":"https://api.github.com/repos/xunit/xunit/issues/434/labels{/name}","comments_url":"https://api.github.com/repos/xunit/xunit/issues/434/comments","events_url":"https://api.github.com/repos/xunit/xunit/issues/434/events","html_url":"https://github.com/xunit/xunit/issues/434","id":87509328,"number":434,"title":"Functions mocked in the constructor are removed when mocking other functions from the same class within a test method ","user":{"login":"ajpasco","id":602302,"avatar_url":"https://avatars.githubusercontent.com/u/602302?v=3","gravatar_id":"","url":"https://api.github.com/users/ajpasco","html_url":"https://github.com/ajpasco","followers_url":"https://api.github.com/users/ajpasco/followers","following_url":"https://api.github.com/users/ajpasco/following{/other_user}","gists_url":"https://api.github.com/users/ajpasco/gists{/gist_id}","starred_url":"https://api.github.com/users/ajpasco/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajpasco/subscriptions","organizations_url":"https://api.github.com/users/ajpasco/orgs","repos_url":"https://api.github.com/users/ajpasco/repos","events_url":"https://api.github.com/users/ajpasco/events{/privacy}","received_events_url":"https://api.github.com/users/ajpasco/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-06-11T21:39:38Z","updated_at":"2015-06-11T23:02:28Z","closed_at":null,"body":"Currently we are using JustMock Lite with xUnit 1.9.2 and have test cases where we create reusable mocks for our test methods within the controller of a test class.  My team bumped into an issue after performing an upgrade to version 2.0.  It appears when using the constructor to mock a method from a class  (for reuse in test methods) and then creating a second mocked function within the test method (which is a different method from the same class that was defined in the constructor) causes the mocked implementation of the method in the constructor to disappear.  \r\n\r\nPlease see the test example below.  In the test class MockIssueExample, I have test methods that verifies the return value from an interface method or virtual function.  In version 1.9.2, the mocks defined in SetupMocks() returns the value of 5 within each of the test methods.  When I upgrade to version 2.0, each of the mocked methods returns a value of 0 in the test method and causes my test to fail.   \r\n\r\nNote: In each of the test methods I left a line of code that fixes the issue but it requires me to copy the mocked implementation from the constructor into the test method.  \r\n\r\nHas something changed in 2.0 that's causing this scoping issue? Can we mock one function within a constructor and a second method within the actual test method without losing the mock implementation that what was defined in the constructor.\r\n\r\nPlease Advise.  Thanks!\r\n\r\n    public class MockIssueExample\r\n    {\r\n        private ITestMethods _testMethods;\r\n        private ClassUnderTest _classUnderTest;\r\n        private ClassTestMethods _classTestMethods;\r\n\r\n        public MockIssueExample()\r\n        {\r\n            SetupMocks();\r\n\r\n            _classUnderTest = new ClassUnderTest(_testMethods, _classTestMethods);\r\n        }\r\n\r\n        private void SetupMocks()\r\n        {\r\n            _testMethods = Mock.Create<ITestMethods>();\r\n            _classTestMethods = Mock.Create<ClassTestMethods>();\r\n\r\n            Mock.Arrange(() => _testMethods.FunctionReturnsInteger_1()).Returns(5);\r\n\r\n            Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_1()).Returns(5);\r\n        }\r\n\r\n        [Fact]\r\n        public void Virtual_function_1_should_return_the_mocked_value()\r\n        {\r\n            //Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_1()).Returns(5);\r\n            Mock.Arrange(() => _classTestMethods.VirtualFunctionReturnsInteger_2()).Returns(6);\r\n\r\n            int returnVal = _classUnderTest.GetValueReturnedFromVirtualFunction_1();\r\n\r\n            Assert.True(returnVal == 5);\r\n        }\r\n\r\n        [Fact]\r\n        public void Interface_function_1_should_return_the_mocked_value()\r\n        {\r\n            //Mock.Arrange(() => _testMethods.FunctionReturnsInteger_1()).Returns(5);\r\n            Mock.Arrange(() => _testMethods.FunctionReturnsInteger_2()).Returns(9);\r\n\r\n            int returnVal = _classUnderTest.GetValueReturnedFromInterfaceFunction_1();\r\n\r\n            Assert.True(returnVal == 5);\r\n        }\r\n    }\r\n\r\n public interface ITestMethods\r\n    {\r\n        int FunctionReturnsInteger_1();\r\n        int FunctionReturnsInteger_2();\r\n    }\r\n\r\n    public class ClassTestMethods\r\n    {\r\n        public virtual int VirtualFunctionReturnsInteger_1()\r\n        {\r\n            return 1;\r\n        }\r\n\r\n        public virtual int VirtualFunctionReturnsInteger_2()\r\n        {\r\n            return 2;\r\n        }\r\n    }\r\n\r\n    public class ClassUnderTest\r\n    {\r\n        private readonly ITestMethods _testMethods;\r\n        private readonly ClassTestMethods _classTestMethods;\r\n\r\n        public ClassUnderTest(ITestMethods testMethods, ClassTestMethods classTestMethods)\r\n        {\r\n            _testMethods = testMethods;\r\n            _classTestMethods = classTestMethods;\r\n        }\r\n\r\n        public int GetValueReturnedFromVirtualFunction_1()\r\n        {\r\n            return _classTestMethods.VirtualFunctionReturnsInteger_1();\r\n        }\r\n\r\n        public int GetValueReturnedFromInterfaceFunction_1()\r\n        {\r\n            return _testMethods.FunctionReturnsInteger_1();\r\n        }\r\n    }"},"comment":{"url":"https://api.github.com/repos/xunit/xunit/issues/comments/111301720","html_url":"https://github.com/xunit/xunit/issues/434#issuecomment-111301720","issue_url":"https://api.github.com/repos/xunit/xunit/issues/434","id":111301720,"user":{"login":"bradwilson","id":16944,"avatar_url":"https://avatars.githubusercontent.com/u/16944?v=3","gravatar_id":"","url":"https://api.github.com/users/bradwilson","html_url":"https://github.com/bradwilson","followers_url":"https://api.github.com/users/bradwilson/followers","following_url":"https://api.github.com/users/bradwilson/following{/other_user}","gists_url":"https://api.github.com/users/bradwilson/gists{/gist_id}","starred_url":"https://api.github.com/users/bradwilson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradwilson/subscriptions","organizations_url":"https://api.github.com/users/bradwilson/orgs","repos_url":"https://api.github.com/users/bradwilson/repos","events_url":"https://api.github.com/users/bradwilson/events{/privacy}","received_events_url":"https://api.github.com/users/bradwilson/received_events","type":"User","site_admin":false},"created_at":"2015-06-11T23:02:28Z","updated_at":"2015-06-11T23:02:28Z","body":"xUnit.net itself uses `Task`s internally, such that the thread your object constructor runs on is not guaranteed to be the same thread that the unit test runs on. Most mocking frameworks use thread locals to stash information, where they really should be using CallContext (which is both thread- and Task-friendly)."}}