nunit

NUnit Mocking not working for Singleton Method

心已入冬 提交于 2019-12-18 07:07:46
问题 Bear with me, I'm new to NUnit. I come from the land of Rails, so some of this is new to me. I have a line of code that looks like this: var code = WebSiteConfiguration.Instance.getCodeByCodeNameAndType("CATALOG_Brands_MinQty", item.Catalog); I'm trying to mock it, like this (assume code is already initialized): var _websiteConfigurationMock = new DynamicMock(typeof(WebSiteConfiguration)); _websiteConfigurationMock.ExpectAndReturn("getCodeByCodeNameAndType", code); When I debug the test,

Reload app.config with nunit

痞子三分冷 提交于 2019-12-18 04:43:05
问题 I have multiple NUnit tests, and I would like each test to use a specific app.config file. Is there a way to reset the configuration to a new config file before each test? 回答1: Try: /* Usage * using(AppConfig.Change("my.config")) { * // do something... * } */ public abstract class AppConfig : IDisposable { public static AppConfig Change(string path) { return new ChangeAppConfig(path); } public abstract void Dispose(); private class ChangeAppConfig : AppConfig { private bool disposedValue =

How do I install the NUnit plugin in Visual Studio 2013 Express?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 04:38:11
问题 I am not able to run the unit tests in Visual Studio 2013 like I used to be able to run it from the Visual Studio window itself in Visual Studio 2010. I am not able to find anything in NuGet Package Manager or on the web. 回答1: Unfortunately this is not possible. The Express SKUs of Visual Studio do not support plugins / extensions. 回答2: Try to use NUnit TestAdapter including NUnit . You will find it searching for NUnit in the Manage NuGet Packages window. I have tested it, and it works fine.

ASP.NET MVC unit testing custom AuthorizeAttribute

淺唱寂寞╮ 提交于 2019-12-18 04:05:07
问题 I'm working on an ASP.NET MVC 4 project (.NET framework 4) and I was wondering how to properly unit test a custom AuthorizeAttribute (I use NUnit and Moq). I overrode 2 methods: AuthorizeCore(HttpContextBase httpContext) and HandleUnauthorizedRequest(AuthorizationContext filterContext) . As you can see, these methods expect an HttpContextBase and AuthorizationContext respectively, but I don't know how to Mock these. This is as far as I got: [Test] public void HandleUnauthorizedRequest

System.BadImageFormatException caused by NUnit project

空扰寡人 提交于 2019-12-18 03:54:54
问题 Good day everyone. I have been having the same problem all day at work and am struggling to find any new paths to go down. I am getting the following error when my solution builds on server. I have no problem running/debugging all tests in the solution and it builds fine. Both server and my PC are x64. I have followed a lot of advice which I have found to no avail. I have set Platform Target to x86 for all projects in my solution under all configurations. I am aware that there is an nunit

Pass parameters via the command line to NUnit

我只是一个虾纸丫 提交于 2019-12-17 23:15:47
问题 Is it somehow possible to pass values to NUnit tests via the command line? My tests use a certain URL. I have different instances of my code at different URLs and would like to specify the URL via the command line. File App.config is not an option, because I want to run the tests for different URLs via a batch file. 回答1: Use an environment variable to pass the information. Use set from the command-line or <setenv> from NAnt. Then read the value using Environment.GetEnvironmentVariable() . 回答2

How to run NUnit from my code

拜拜、爱过 提交于 2019-12-17 22:20:05
问题 I'd like to use NUnit to run unit tests in my plug-in, but it needs to be run in the context of my application. To solve this, I was trying to develop a plug-in that runs NUnit, which in turn will execute my tests in the application's context. I didn't find a specific documentation on this subject so I dug a piece of information here and there and I came out with the following piece of code (which is similar to one I found here in StackOverflow): public static void Main() { SimpleTestRunner

How to run unit tests in STAThread mode?

佐手、 提交于 2019-12-17 17:59:40
问题 I would like to test an app that uses the Clipboard (WindowsForms) and I need the Clipboard in my unit tests also. In order to use it, it should run in STA mode, but since the NUnit TestFixture does not have a main method, I don't know where/how to annotate it. 回答1: For NUnit 2.2, 2.4 (See simple solution below for 2.5): Add an app.config file to the project containing your unit tests and include the following: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections>

Run GC.Collect synchronously

会有一股神秘感。 提交于 2019-12-17 16:43:20
问题 GC.Collect appears to start the garbage collection in a background thread, and then return immediately. How can I run GC.Collect synchronously -- i.e., wait for the garbage collection to complete? This is in the context of NUnit tests. I tried adding the gcConcurrent setting to my test assembly's app.config file, and I tried the same with nunit.exe.config. Neither had any effect -- when I debug, I can still see the finalizer being run on the "GC Finalizer Thread", rather than the thread that

MOQ - Mocking MVC Controller's Response.Cookies.Clear()

流过昼夜 提交于 2019-12-17 16:34:32
问题 I am new to MOQ, but am using it with NUnit for unit testing. I have all parts of my controller mocked, except the following line which throws an 'Object not set to an instance of an object' error message. Response.Cookies.Clear(); I have the following extension method to mock the controller context which works for everything else I have come accross so far (very much thanks to the good people on this forum). public static int SetUpForTest(this System.Web.Mvc.Controller ctl, string username,