moq

MVC Testing using a mocking framework (Moq)

感情迁移 提交于 2019-12-25 02:50:31
问题 I'm using Moq to help in testing my ASP.NET MVC2 application. Problem: ArgumentException was unhandled by user code. Unable to obtain public key for StrongNameKeyPair This code has been adapted from Scott Hanselman's NerdDinner1. HomeController CreateHomeControllerAs(string userName) { var mock = new Mock<ControllerContext>(); mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(userName); // fails here mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true); var

MVC 3 Unit Test - Get Actual Response Data

守給你的承諾、 提交于 2019-12-25 01:38:41
问题 All, I'm developing and unit testing an interactive voice application using ASP.NET MVC 3 whose controllers return Views containing VoiceXML. I'd like to create unit tests that capture the actual VoiceXML output so I can schema-validate it. My reading and testing have taken me to Scott H's FakeHttpContext that uses Moq, as well as several responses here. Everything compiles correctly, and I'm trying to do something like the following: [TestMethod] public void WelcomeTest1() {

Moq newbie test setup

限于喜欢 提交于 2019-12-25 01:09:57
问题 I'm coming up to speed with the Moq framework and I have a button on a form that launches another form. The client wants the form to launch whether the Click or DoubleClick event is fired off. My understanding of the Moq framework is that it is useful when you want to simulate certain types of actions: (For brevity, I'm only listing a few types of actions) Connect to a database Write to a file Determine network connectivity But I'm not 100% certain on how Moq can interact with Windows

How do I unit test a void function that inserts into the database and saves?

纵然是瞬间 提交于 2019-12-25 00:52:15
问题 I have this function in my interface and it creates a new PlayersBadge entry in the db. I have to write unit tests for and I'm stuck. public void Badge(int pID, int bID, int gID = 0) { var list = EliminationDbContext.PlayerBadges.Where(x=>x.Badge.BadgeID.Equals(bID) && x.Player.PlayerID.Equals(pID)); //if player doesn't have Badge create new Badge if (list.Any() != true) { PlayerBadge b = new PlayerBadge { PlayerID = pID, BadgeID = bID, DateEarned = DateTime.Today, GameID = gID };

moq, how to test the save method?

喜夏-厌秋 提交于 2019-12-25 00:30:31
问题 I have a save() method, not really sure how to test. below is my code. public interface IRepository<T> { T Get(int id); void Save(T item); void Delete(int id); } save method doesn't return any values back, I cannot compare the value. however, I already have 4 users, after adding another one, I only check the total number of users, is it enough to test it? [Test] public void Add_a_new_smoothie_user_should_return_total_5_users() { // Arrange var totalUsers = _users.Count(); _mockUserRepository

How do I unit test the method which has dependency on base classes?

荒凉一梦 提交于 2019-12-24 19:54:21
问题 I have a below class, which having base class and I am trying to write unit test. public class CarService : ServiceBase, IProvisioningService { private IAuditRepository _repository; public CarService(IHostingFactory hostingFactory) : base(name, hostingFactory) { } public override void DoWork() { if (_repository == null) { //its calling the base method. _repository = CurrentContext.ContainerFactory.GetInstance<IAuditRepository>(); try { _repository.Insert("something"); } catch (Exception ex) {

how to mock/inject a getter into a system under test?

旧巷老猫 提交于 2019-12-24 15:48:56
问题 Mark provides an elegant answer to a related question. My class has a read only property: public class myclass { ... public virtual string Devicelocation => Message.Message.Items[0].ToString(); ... public someMethod() { if(Devicelocation=="YourMom") { //dostuff } else { //dootherstuff } } } I would like to execute someMethod() with an assumption for what Devicelocation is equivalent to. How do I mock or inject a value into Devicelocation? 回答1: You can set up Devicelocation like this: var stub

Moq Return using Where()

随声附和 提交于 2019-12-24 14:42:37
问题 I'm trying to set up a mock to return data from a collection: private IList<DutyCategory> dutyCategories; private Mock<IDutyCategoryService> mockService; [TestInitialize()] public void UnitTestSetup() { dutyCategories = new List<DutyCategory>() { new DutyCategory(){Description = "Description",ID = 1,IsActive = true,Name = "Test 1",OrganizationID = 1} }; mockService = new Mock<IDutyCategoryService>(); mockService.Setup(a => a.GetDutyCategories()).ReturnsAsync(dutyCategories); mockService.Setup

Testing console output using a mock TextWriter

廉价感情. 提交于 2019-12-24 14:29:28
问题 I am trying to write a unit test for a class that outputs to the console. To capture the console output from this class I decided to create a mock TextWriter object and redirect the console output to it within the test class. [TestMethod] public void TestMethod() { var outLines = new List<string>(); var mockWriter = new Mock<TextWriter>(); mockWriter.Setup(writer => writer.WriteLine(It.IsAny<string>())) .Callback<string>(s => outLines.Add(s)); Console.SetOut(mockWriter.Object); //

Mocking a repository with Moq

三世轮回 提交于 2019-12-24 12:22:54
问题 To mock a repository, I use the code below. I don't understand why the variable empl is always null . Do you know what I missed ? Thanks, [TestMethod] public void Test() { var employee = new Employee { EmployeeID = 1, Code = "Code", FirstName = "MyFirstName", LastName = "MyName" }; var employeeRepository = new Mock<IEmployeeRepository>(); employeeRepository.Setup(x => x.Add(employee)).Verifiable(); var employeeService = new EmployeeService(employeeRepository.Object); var empl =