How do I undo a Setup call for a moq Mock? - nunit

This might be a special use case that I am dealing with here. Here is what my simple C# NUnit test that uses Moq looks like
Mock<ISomeRepository> mockR = new Mock<ISomeRepository>();
mockR.Setup(x => x.GetSomething).Returns(new Something(a=1,b=2);
--use the mocked repository here
Now later in this same unit test or another test case I want to invoke the real implementation of the method GetSomething() on this mockR object.
Is there a way to do that? My repository is Singleton at its heart. So even if I create a new object, the GetSomething method still returns the same Moq'd object.

That would largely depend on your implementation of that GetSomething, which is something you're not showing here ;). Also, I'm not sure that's even a valid setup, shouldn't there be a .Setup(..).Returns(..) there?

Mocks are used to represent dependencies of a class allowing that class to be tested without using their actual dependencies. Or you can do tests which involve the actual dependencies.
But using a mocked dependency and the real dependency within the same unit test sounds like you're not clear what your test is testing.
If it's another test case, it shouldn't be a problem either. Each test should not impact another, so if you set up the class under test separately that should be fine, even with a singleton.
I'm assuming that you're injecting the singleton dependency. If not, do that.

Related

Unit Testing for pgxpool

I am looking to write unit tests for Go code that uses pgxpool to interact with a postgres database. Is there a test framework that will stand up a dummy or mock database to test on?
I am aware of a package called pgxpoolmock. The problem is, there is no way to test actual production code, as a pool from pgxpool cannot be casted to the type pgxpoolmock (or can it?)
pgxpoolmock is using an interface pgxpoolmock.PgxPool. This interface has all the methods that are being used in the pgxpool.Pool struct. pgxpoolmock.MockPgxPool is implementing that interface. pgxpoolmock.NewMockPgxPool(ctrl) returning a struct that implements the interface pgxpoolmock.PgxPool. So if you want to use pgxpoolmock, you have to use pgxpoolmock.PgxPool in your application code not in test code alone.

How to create a scala class based on user input?

I have a use case where I need to create a class based on user input.
For example, the user input could be : "(Int,fieldname1) : (String,fieldname2) : .. etc"
Then a class has to be created as follows at runtime
Class Some
{
Int fieldname1
String fieldname2
..so..on..
}
Is this something that Scala supports? Any help is really appreciated.
Your scenario doesn't seem to make sense. It's not so much an issue of runtime instantiation (the JVM can certainly do this with reflection). Really, what you're asking is to dynamically generate a class, which is only useful if your code makes use of it later on. But how can your code make use of it later on if you don't know what it looks like? For example, how would your later code know which fields it could reference?
No, not really.
The idea of a class is to define a type that can be checked at compile time. You see, creating it at runtime would somewhat contradict that.
You might want to store the user input in a different way, e.g. a map.
What are you trying to achieve by creating a class at runtime?
I think this makes sense, as long as you are using your "data model" in a generic manner.
Will this approach work here? Depends.
If your data coming from a file that is read at runtime but available at compile time, then you're in luck and type-safety will be maintained. In fact, you will have two options.
Split your project into two:
In the first run, read the file and write the new source
programmatically (as Strings, or better, with Treehugger).
In the second run, compile your generated class with the rest of your project and use it normally.
If #1 is too "manual", then use Macro Annotations. The idea here is that the main sub-project's compile time follows the macro sub-project's runtime. Therefore, if we provide the main sub-project with an "empty" class, members can be added to it dynamically at compile time using data that the macro sees at runtime. - To get started, Modify the macro to read from a file in this example
Else, if you're data are truly only knowable at runtime, then #Rob Starling's suggestion may work for you as it did me. I'll share my attempt if you want to be a guinea pig. For debugging, I've got an App.scala in there that shows how to pass strings to a runtime class generator and access it at runtime with Java reflection, even define a Scala type alias with it. So the question is, will your new dynamic class serve as a type-parameter in Slick, or fail to, as it sometimes does with other libraries?

Is there a workaround for setting [HostType("Moles")] when dealing with anonymous methods in MSpec?

I'm using Pex and Moles for my low-level unit testing, but I'm also exploring MSpec for business-logic validation, and would like to keep using Moles for consistency. The problem, I think, is that MSPec uses anonymous methods, so there's no way to apply the HostType("Moles") attribute. For example:
Because of = () =>
employeeList = EmployeeManager.GetUsersByRoles(rolesToLoad);
It should_return_a_list_of_employees = () =>
employeeList.ShouldNotBeNull();
I'm mocking the Roles provider called inside "GetUsersByRoles," and when I try to run this test via MSpec, I get the standard "Moles requires tests to be IN an instrumented process" error, with the instruction to add [HostType("Moles")] to my test method. Is there any workaround or other option available here?
Side note: I have downloaded MSMSpec.tt and modified it to include the attribute on the generated VSTests, but I'd like to be able to run the MSpec tests directly via its own runner or TestDriven.net so I can get the friendly output for BAs and business owners.
The workaround is to replace the anonymous method with one that is not. Moling Mspec is basically not possible.
Moles is not capable of detouring anonymous methods. The reason why is that the methods must be addressable, to be detoured. Anonymous methods are not implicitly addressable, because they are generated and referenced during runtime. Simply put, you can not call an anonymous method through the class, because it is, well... anonymous.
The Moles Manual states, "Moles can be used to detour any .NET method, including non-virtual and static methods in sealed types." Therefore, operating under the assumption that Moles uses reflection to identify class members is a safe bet. Anything that can not be called via delegate, Action, or Func, can not be moled.

Executing Scala objects in Eclipse without a main method

I have an assignment to code several methods in Scala. The methods will be encapsulated in an object that has no main method. The professor gave us a JAR file that contains an interface (my object implements this interface) as well as a sort of pseudo test object that performs various assert statements against each of my functions. This object also does not contain a main method.
Now in Intellij I simply had to declare the dependency on the JAR in the classpath, and it runs fine. Eclipse is giving me trouble though because when I go to define a Scala application run configuration it specifically asks me to name the class that contains a main method, and there is no main method.
I am assuming that I might be choosing the wrong project type for this type of set up, but I am inexperienced with this and I would appreciate any advice you might have for running something like this in eclipse.
Thanks.
I would either:
just write an object with a main method which calls the test object, or
start a Scala interpreter in your project (from context menu, under Scala).
Preferring the first approach, because it's faster to repeat tests after a modification.

How does Import and Export work at Runtime in MEF?

I am starting to learn, MEF and one important thing in it is that I can mark some item (class, propety,method) with Export attribute so that, who ever wants use it will create Import attribute on an instance varaible and use it. How does this mapping happen and when does it happen? Is the import happen lazily on demand or all the composition happen at the start up? Sorry for the ignorant question, I am trying to understand the flow.
It happens in a phase called "Composition". First you create a container and load all your possible sources of parts into it, and then you Compose it. When you do the composition, it resolves all the dependencies and throws an exception if it can't resolve them all properly.
In general, your parts get instantiated during composition (and if you set a break point in the constructor of your part classes, you will see the break point hit during your call to Compose()). However, you can override this in a straightforward way if you use Lazy<T> as the type of your import (assuming you exported your part as type T).
To see how the composition works, take a look at the Compose() method here.