mocking

Teaching testing habits to other developers?

Developers have many different options out there to create fast and relatively maintainable unit test suites. But this takes a great deal of knowledge involved in decoupling modules of code, isolating the code under test within its test context, and using test doubles (stubs, fakes, mocks). What's more confusing is that within the conc...

How to mock a String using mockito?

I need to simulate a test scenario in which I call the getBytes() method of a String object and I get an UnsupportedEncodingException. I have tried to achieve that using the following code: String nonEncodedString = mock(String.class); when(nonEncodedString.getBytes(anyString())).thenThrow(new UnsupportedEncodingException("Parsing erro...

Reliable mocking framework for ActionScript?

I'm looking for a reliable mocking framework for ActionScript. I've been using mock-as3, but I'm annoyed with what I feel is a hack-ish solution for triggering events. There are other a few other reasons why I'd like to have some options, but not sure if I necessarily need to go into them. I've also looked into Mock4AS, but the interface...

Using Mockito to test abstract classes

I'd like to test an abstract class. Sure, I can manually write a mock that inherits from the class. Can I do this using a mocking framework (I'm using Mockito) instead of hand-crafting my mock? How? ...

How do you mock out the file system in C# for unit testing?

Are there any libraries or methods to mock out the file system in C# to write unit tests? In my current case I have methods that check whether certain file exists and read the creation date. I may need more than that in future. ...

Do I need to write a unit test for a method within service class that only calls a method within repository class?

Example I have a repository class (DAL): public class MyRepository : IMyRepository { public void Delete(int itemId) { // creates a concrete EF context class // deletes the object by calling context.DeleteObject() } // other methods } I also have a service class (BLL): public class MyService { pri...

Entity Framework context mock/fake

It should parse EDMX file and create a moch/fake to use in Unit tests. The easiest integration would be by using T4 that we already have in VS IDE. Has anybody seen it on the web? Or is maybe writing it on their own? Or is there an OSS in progress doing this? Anybody? ...

Best practices for HttpContext and testable controlers in ASP.Net MVC

Update: Based on a couple of the answers I have received, I just want to make clear that I am well aware how to go about mocking HttpContext using a mocking framework. I am more interested knowing what the pros and cons of mocking HttpContext are when compared to using wrapper classes around HttpContext. I'm looking for opinions on h...

Does Moq ignore namespaces when setting up mocks?

The second assertion never executes in the unit test below: namespace Foo { public class MyClass { } } namespace Bar { public class MyClass { } } namespace Quux { public interface IRepo { object Get<T>() where T : new(); } } namespace Tests { [TestFixture] public class MyTests { private Mock<...

How can i unit test an object internal to a method in Objective-C?

I'm wondering how to go about testing this. I have a method that takes a parameter, and based on some properties of that parameter it creates another object and operates on it. The code looks something like this: - (void) navigate:(NavContext *)context { Destination * dest = [[Destination alloc] initWithContext:context]; if (conte...

The value of high level unit tests and mock objects

I am beginning to believe that unit testing high level, well-written code, which requires extensive use of mock objects, has little to no value. I am wondering if this assertion is correct, or am I missing something? What do I mean by high level? These are the classes and functions near the top of the food chain. Their input and output ...

Unit Testing Adding to Zip / Extracting from Zip

I have the following code for adding to/extracting from Zip. I'm trying to refactor this to make it test-ready. Can someone provide pointers on how I can accomplish this? Aside: I'm using Moq as my mock framework and MSTest as my Unit Testing tool private const long BufferSize = 4096; public static void ExtractZip(string zipFilename...

Verify that either one method or the other was invoked in a unit test

Example: public bool Save(MyObj instance) { if (instance.IsNew) { this.repository.Create(instance); } else { this.repository.Update(instance); } } How do I create a test in Moq that verifies: that a property IsNew is being read that either Create() or Update() has been invoked ...

Exercises to enforce good practices such as TDD and Mocking.

I'm looking for resources that provide an actual lesson plan or path to encourage and reinforce programming practices such as TDD and mocking. There are plenty of resources that show examples, but I'm looking for something that actually provides a progression that allows the concepts to be learned instead of forcing emulation. My pri...

Mocking and DetachedCriteria in unit tests

How would you test the following code? public IList<T> Find(DetachedCriteria criteria) { return criteria.GetExecutableCriteria(session).List<T>(); } I would like to mock NH implementation (like setting mocks for ISession, ISessionFactory etc.) but I am having trouble with this one. ...

When to Expect and When to Stub?

I use NMock2, and I've drafted the following NMock classes to represent some common mock framework concepts: Expect: this specifies what a mocked method should return and says that the call must occur or the test fails (when accompanied by a call to VerifyAllExpectationsHaveBeenMet()). Stub: this specifies what a mocked method should r...

Unit testing Abstract classes in Groovy

I am new to unit testing and mocking. I am trying to unit test an abstract domain class in Grails. How should I mock an implementation so I can unit test the constraints of the domain class? Is there a way to use the mock libraries that come with groovy or grails? Should I just implement a class that simply extends the abstract class? ...

mockForConstraintsTests abstract groovy class

Unit testing Abstract classes in Groovy I asked a question previous about unit testing and mocking a domain class, but I do not think I was specific enough. I have a domain class: package toplevel.domain abstract class Party { static hasMany = [roles:PartyRole] static constraints = { roles(nullable:true) dateCr...

Testing Nested Error Handling with OCMock

I'd really appreciate some advice around testing for errors using OCMock. Method Under Test It grabs a process using an instance of the AppScriptController class. @implementation ProcessGrabber -(void)grabProcess { NSError *error = nil; NSString *processName = [appScriptController processName:ProcessRef ...

Swap method implementation at runtime in .NET?

How can i change the implementation of a method at runtime from return false; to return true;? I don't have control over the methods implementation as it comes with a third-party library. Any workaround is appreciated. Again: I do not control neither the method itself nor it's callers. ...