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

System.InvalidProgramException : Cannot create boxed ByRef-like values. #1286

Closed
RohitBinjola opened this issue Sep 1, 2022 · 1 comment
Closed

Comments

@RohitBinjola
Copy link

RohitBinjola commented Sep 1, 2022

Getting below issue in MOQ unit test after upgrading the framework from 3.1 to 6.

Message:
System.InvalidProgramException : Cannot create boxed ByRef-like values.

Stack Trace:

StreamProxy.Write(ReadOnlySpan`1 buffer)
StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
StreamWriter.Flush()

Test Code: (Same code is working fine in .net core 3.1 but failing with below exception in .net 6)

public void StreamWriter_Test()    
{
            var stream = new Mock<Stream>();
            stream.Setup(s => s.CanWrite).Returns(true);
            using var sw = new StreamWriter(stream.Object);
            sw.WriteLine("Test");
            sw.Flush();
}

image

@stakx
Copy link
Contributor

stakx commented Oct 3, 2022

It would appear that StreamWriter.WriteLine calls its underlying stream's Write(ReadOnlySpan<> buffer) method, which Moq cannot mock (because its underlying library, DynamicProxy, cannot intercept it). That's an architectural limitation in both DynamicProxy and Moq; there's nothing that can be done about it. You'll likely need to create a mock Stream manually.

@stakx stakx closed this as completed Oct 3, 2022
@devlooped devlooped locked and limited conversation to collaborators Sep 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants