mocking

Should mock objects dispatch events?

I'm using this kind of wiring for my MVC and I want to test the controller. So far, I've found two ways : either my mock view dispatch events or my controller expose public methods. Dispatching events seems the way to go (as pointed out in an answer), but if my view is a simple mock object, how do I dispatch those events? ...

Mocking Membership

I'm writing a custom Profile provider, but I still intend to use the default AspNetSqlMembershipProvider as my Membership provider. My GetAllProfiles() method in my Profile provider looks like this: 1 public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, o...

jMock Mocking Classes and Interface

I was experimenting jMock as my mocking framework for my project. I came into a situation where I need to mock both a class and an interface. I used the ClassImposteriser.INSTANCE to initiate the impostor of the context. Supposing a class Validator and an interface Person to mock. When I was going to mock the Interface Person, I ran to ...

Mocking the is operator in Moq

Is there a way to get my mocks to impersonate a type? I am trying to do something like this: var myMock = new Mock<IMyType>(); myMock.Setup(x => x.GetType()).Returns(typeof(MyTypeImpl)); however, GetType is not overrideable. Any suggestions? ...

How to mock the Request on Controller in ASP.Net MVC ?

I have a controller in C# using the ASP.Net MVC framework public class HomeController:Controller{ public ActionResult Index() { if (Request.IsAjaxRequest()) { //do some ajaxy stuff } return View("Index"); } } I got some tips on mocking and was hoping to test the code with the following ...

How do I unit test an ASP.NET MVC controller that uses DotNetOpenId?

I have an AccountController whose constructor takes an object derived from my custom IOpenIdAuthentication interface. By default, this is an OpenIdAuthenticationService object that wraps an OpenIdRelyingParty. The interface looks like this: public interface IOpenIdAuthentication { IAuthenticationResponse Response { get; } IAuthe...

mock problem

//IsExist always false,is it a bug? [TestMethod] public void IsExist() { private Mock<IRepository> repository = new Mock<IRepository>(); Foo f = new Foo(); repository.Expect(s => s.IsExist(foo)).Returns(true); var controller = new MyController(repository.Object); var res...

How to mock for testing in Haskell?

Suppose I am defining a Haskell function f (either pure or an action) and somewhere within f I call function g. For example: f = ... g someParms ... How do I replace function g with a mock version for unit testing? If I were working in Java, g would be a method on class SomeServiceImpl that implements interface SomeService. ...

How to set-up mock object to return one result at first call and Exception for the rest

I'm using Moq library. I'm mocking up an instance that does all regular CRUD functionality. I would like to set it up in a way to allow only one Delete(x) call over some object and all consecutive calls to Delete(x) of the same object should return an Exception. My Delete() method returns void. How do I do that? Some code mock = new ...

Groovy: Verify construction of stubbed URL

The test class below verifies that a simple HttpService gets content from a given URL. Both the implementations shown make the test pass, though one is clearly wrong because it constructs the URL with an incorrect argument. To avoid this and correctly specify the behaviour I want, I'd like to verify that in the use block of the test ca...

MOQ: Returning value that was passed into a method

I have a method on an interface: string DoSomething(string whatever); I want to mock this with MOQ, so that it returns whatever was passed in - something like: _mock.Setup( theObject => theObject.DoSomething( It.IsAny<string>( ) ) ) .Returns( [the parameter that was passed] ) ; Any ideas? ...

The Purpose of Mocking

What is the purpose of mocking? I have been following some ASP.NET MVC tutorials that use NUnit for testing and Moq for mocking. I am a little unclear about the mocking part of it though. ...

Is it possible to extend a class with only private constructors in Java?

For unit testing purposes I'm trying to write a mock object of a class with no constructors. Is this even possible in Java, of is the class simply not extensible? ...

Rhino Mocks & Compact Framework

Hi I've been experimenting with Rhino Mocks for unit testing my .Net Compact Framework application, and have hit a problem. I can get a basic unit test using Rhino Mocks built, but every time I run the test that has the Rhino Mocks code in it, the test fails because it can't find the Rhino Mocks assembly. System.TypeLoadException: Coul...

How can I stub or mock the request.subdomains method in Rails?

I am trying to write some functional tests in my rails app, and in the application_controller.rb I have this: before_filter :current_account def current_account @current_account ||= Account.find_by_subdomain!(request.subdomians.first) end When running tests, request.subdomains doesn't contain the valid subdomains I'm looking for and...

Using typemock for chaining

I am just starting here with mocking about, and I am trying something which I think should be quite simple. I have a class that reads Google calendar data. First, it creates a CalendarService object, then it calls Query on that object, receives a EventFeed and iterates on the Item collection of AtomEntryCollection. I want it all to be m...

How to set the ErrorCode of a ManagementException?

I want to handle a ManagementException exception for a specific ErrorCode only and am having trouble writing the unit test for it. Ordinarily, I would write the test so that it is something like the following: Searcher search = MockRepository.GenerateMock<Searcher>(); // wrapper for ManagementObjectSearcher ... search.Expect(s => s.G...

How to use TypeMock Isolator with Linq to SQL and DataContext

Hi, i am using TypeMock Isolator to fake the tables on my DataContext like this: Isolate.Swap.CallsOn(ctx.GetTable<TTable>()).WithCallsTo(content); I now can fill the "database" with arbitrary data on each test. In order to get meaningful data for the test scenario into the DataContext i usually have to create 3-5 objects. Now, the p...

Using groovy to mock wrapped class

I am currently trying to go back through and write unit tests for some code that wraps an existing class. The function I am looking for has code that looks like the following... private OldObject oldObject ... public Boolean method(){ Boolean returnValue = false if(oldObject.method(100)){ returnValue = true } if(oldObject....

How do you mock class with readonly property?

Any idea in Moq for a class with readonly modifier : Class myClass { private int id; public int Id{ get {return id;}} public myClass(int id) { this.id = id } } I was trying to mock this object: var myMock= new Mock<myClass>(); myMock.SetupGet(m => m.ID).Return(555); It returned me error: System.ArgumentException: I...