Any alternative to shim feature provided in Microsoft fake framework? - microsoft-fakes

I am wondering is there any alternative lib to the shim feature provided in Microsoft fake framework since it is only supported in ultimate version?

There are three frameworks to my knowledge that allow you to mock non virtual methods and sealed classes like Fakes' Shims. There are
Microsofts' Moles or Fakes
Teleriks JustMock
TypeMocks
They are all commercial because they use the Profiling API, which is very hairy and poorly documented, so coding them is a real pain.
And for the record I'm all for fakes. Most code that people are working on is legacy code. One of the Pragmatic Programmer's rules of refactoring is make sure that you have unit test coverage before any refactorings to avoid regression. This makes Fakes and similar frameworks super useful, especially when the legacy code was not written for test-ability.

Prig hasn't been updated to work with VS 2017, but Pose does, and works really well for what I needed it for (basic shimming of Environment.UserName, and DateTime.Now and similar), and has a really nice interface:
// Create shims. They only apply within this isolate block.
var dateTimeShim = Shim.Replace(() => DateTime.Now)
.With(() => new DateTime(2010, 1, 1));
var usernameShim = Shim.Replace(() => Environment.UserName)
.With(() => "john.wick");
// Shims are only active within an Isolate block - and you
// have to pass all shims you want to be active.
PoseContext.Isolate(() =>
{
// Run your test - shims are active at this point.
RunTest();
}, dateTimeShim, usernameShim);
EDIT
I should note that I get lots of errors when doing fairly basic tests - really scary errors like "Common Language Runtime detected an invalid program.", and "JIT Compiler encountered an internal limitation." so caveat emptor.

An open source alternative is Prig. MIT licenced and still active - but slightly behind with VS IDE

Related

Dynamically register a language

I'm writing a vscode extension and I'd like to register languages dynamically, based on user configuration. The extension would then instantiate LSP clients to talk to servers derived from user configuration as well.
This would allow for people writing custom and toy languages to get an extension "for free" and experiment with editor features without necessarily having to implement and publish the vscode part of it.
I've dug a bit in the vscode sources, and found an interface that seem like it could help : "ILanguageService", but I'm unsure as to whether this is something that's accessible from the extension API.
Any idea how I could go at it ? Is it even possible ?
Alright, so my question stemmed from a misunderstanding of how LSP clients work. They don't necessarily need to be tied to a language, and can work on a glob-pattern basis, something like
const filter: DocumentFilter = {
scheme: 'file',
pattern: `**/*.${myLanguage.extension}`
};
const clientOptions: LanguageClientOptions = {
documentSelector: [filter]
};
This seems to be sufficient for vscode to understand which LSP it should be calling

How do I use such a line in Kotlin?

I use Python, but I don't know how it works in Kotlin. This is an example
example => exec("""print("hello")""") output => hello
exec("""print("hello")""") output => hello
Kotlin supports JSR-223. You can use the jvm scripting engine to eval kts files.
val engine = ScriptEngineManager().getEngineByExtension("kts")
engine.eval("""print("hello")""")
You need JSR-223 library dependency. Refer to example
implementation("org.jetbrains.kotlin:kotlin-scripting-jsr223:$kotlinVersion")
Short answer: this isn't practical in Kotlin.
Technically, there may be ways, but they're likely to be far more trouble than they're worth; you're far better looking for a different approach to your problem.
Unlike a dynamic (‘scripting’) language like Python, Kotlin is statically-compiled. In the case of Kotlin/JVM, you run the Kotlin compiler to generate .class files with Java bytecode, which is then run by a JVM.
So if you really need to convert a string into code and run it, you'd have to find a way to ensure that a Kotlin compiler is available on the platform where your code is running (which it often won't be; compiled bytecode can run on any platform with a JVM, and most of those won't have Kotlin installed too). You'd then have to find a way to run the compiler; this will probably mean writing your source code out to a file, starting up the compiler program as a separate process (as I don't think there's an API for calling it directly), and checking the results. Then you'd have find the resulting bytecode and load into the JVM, which will probably mean setting up a separate classloader instance.
All of which is likely to be slow, fragile, and very awkward.
(See these previous questions which cover some of the same ground.)
(The details will be different for Kotlin/JS and Kotlin/Native, but I think the principles are roughly the same.)
In general, each computer language has its own approach, its own mind-set and ways of doing things, and it's best to try to understand that and accept that patterns and techniques from one language don't always translate well into another. (In the Olden Days™, it used to be said that a determined programmer could write FORTRAN programs in any language — but only in satire.)
Perhaps if you could explain why you want to do this, and what sort of problem you're trying to solve (probably as a separate question), we might be able to suggest more natural solutions in Kotlin.

Is there a Rhinomock version of AutoConfiguredMoqCustomization?

I am bound to use Rhinomocks in my current project. Is there a way of setting up using Autofixture with Rhinomocks to achieve the same behaviour as when combining AutoFixture with Moq using the AutoConfiguredMoqCustomization customization?
Currently I have to do a lot of dummy mock setups in my test code as seen below. In the code i dont really care about the locations that I create. I just want the GetAllLocations method to return a random list of locations instead of null:
var f = new Fixture();
var masterDataProvider = MockRepository.GenerateStub<IMasterDataProvider>();
masterDataProvider.Stub(x => x.GetAllLocations()).Return(f.CreateMany<Location>());
f.Inject(masterDataProvider);
The various Glue Libraries for AutoFixture don't have full feature parity. AutoConfiguredMoqCustomization is one of the features that are only partially ported to other Glue Libraries. IIRC, it's only available for Moq and one other dynamic mock library.
So the short answer is that no, this feature isn't available for Rhino Mocks.
Original answer:
Yes, you can use AutoFixture.AutoRhinoMocks instead of AutoFixture.AutoMoq. It works the same way, but some of the type names are different - for example, the ICustomization you should use is called AutoRhinoMockCustomization.

TestCase scripting framework

For our webapp testing environment we're currently using watin with a bunch of unit tests, and we're looking to move to selenium and use more frameworks.
We're currently looking at Selenium2 + Gallio + Xunit.net,
However one of the things we're really looking to get around is compiled testcases. Ideally we want testcases that can be edited in VS with intellisense, but don't require re-compilling the assembly every single time we make a small change,
Are there any frameworks likely to help with this issue?
Are there any nice UI tools to help manage massive ammount of testcases?
Ideally we want the testcase writing process to be simple so that more testers can aid in writing them.
cheers
You can write them in a language like ruby (e.g., IronRuby) or python which doesnt have an explicit compile step of such a manner.
If you're using a compiled a compiled language, it needs to be compiled. Make the assemblies a reasonable size and a quick Shift F6 (I rewire it to shift Ins) will compile your current project. (Shift Ctrl-B will typically do lots of redundant stuff). Then get NUnit to auto-re-run the tests when it detects the assembly change (or go vote on http://xunit.codeplex.com/workitem/8832 and get it into the xunit GUI runner).
You may also find that CR, R# and/or TD.NET have stuff to offer you in speeding up your flow. e.g., I believe CR detects which tests have changed and does stuff around that (at the moment it doesnt support the more advanced xunit.net testing styles so I dont use it day to day)
You wont get around compiling test frameworks if you add new tests..
However there are a few possibilities.
First:
You could develop a native language like i did in xml or similar format. It would look something like this:
[code]
action name="OpenProfile"
parameter name="Username" value="TestUser"
[/code]
After you have this your could simply take an interpreter and serialize this xml into an object. Then with reflection you could call the appropriate function in the corresponding class. After you have a lot of actions implemented of course perfectly moduled and carefully designed structure ( like every page has its own object and a base object that every page inherits from ), you will be able to add xml based tests on your own without the need of rebuilding the framework it self.
You see you have actions like, login, go to profile, go to edit profile, change password, save, check email etcetc. Then you could have tests like: login change password, login edit profile username... and so on and so fort. And you only would be creating new xmls.
You could look for frameworks supporting similar behavior and there are a few out there. The best of them are cucumber and fitnesse. These all support high level test case writing and low level functionality building.
So basically once you have your framework ready all your have to do is writing tests.
Hope that helped.
Gergely.

Has anyone created any cool rules for FxCop/StyleCop?

I'm just looking for some inspiration. Especially in the area of performance and security, naming conventions are important but not as 'cool' ;)
Even if your rule was only applicable to your domain/project but demonstrates how powerful a rule can be, please let me know.
I work with C#, but I'm interested in rules for any language.
In my experience, the developers that have started out creating custom rule sets for FxCop, usually give up after pulling out much hair. It seems like a great idea, but the pain is just not worth the effort.
An alternative to the mess of writing FxCop custom rules would be to use the commercial tool NDepend. With this tool one can write Code Rule over LINQ Queries (namely CQLinq). Disclaimer: I am one of the developers of the tool
More than 200 code rules are proposed by default, these include naming conventions, design, architecture, code quality, code evolution, dead code, .NET Fx usage...
CQLinq is dedicated to write code rules that can be verified live in Visual Studio, or that can be verified during build process and reported in an HTML/javascript report.
The strength of CQLinq over FxCop API or other tools, is that it is straightforward to write a code rule, and get immediately results. Facilities are proposed to browse matched code elements. Concretely this looks like that:
I've got a nice functioning base with 2 rules so far at breusable.codeplex.com under the fxcop directory
ConfigKeyExistsInConfig (make sure that any references to ConfigurationManager with a magic string key actually exists in the config file.
NoUnderscoresInProperties
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.FxCop.Sdk;
namespace RulesByImaginaryDevelopment
{
public class NoUnderscoresInProperties : BaseRule
{
public NoUnderscoresInProperties() : base("NoUnderscoresInProperties") { }
public override ProblemCollection Check(Member member)
{
var prop = member as PropertyNode;
if(prop==null)
return Problems;
if(prop.Name.Name.Contains("_"))
{
Problems.Add(new Problem(new Resolution("Remove any '_' from name "+prop.Name.Name)));
}
return Problems;
}
}
}
Also http://msdn.microsoft.com/en-us/magazine/cc163930.aspx