stubbing

How to use the real parameters when creating a stub method in RhinoMocks?

◇◆丶佛笑我妖孽 提交于 2021-02-06 11:28:03
问题 I want to create a stub of the following interface: interface IUnitOfWork { void DoInTransaction(Action method); } In the stub object, all I want DoInTransaction to do is run method() . Something like: // pseudo-code unitOfWorkStub.Stub(x => x.DoInTransaction(method)).Do(method()) Is it possible to create this kind of a stub with RhinoMocks? How can this be done? 回答1: use this: unitOfWorkStub.Stub(x => x.DoInTransaction(Arg<Action>.Is.Anything)) .WhenCalled(x => ((Action)x.Arguments[0])());

How to use the real parameters when creating a stub method in RhinoMocks?

泄露秘密 提交于 2021-02-06 11:25:14
问题 I want to create a stub of the following interface: interface IUnitOfWork { void DoInTransaction(Action method); } In the stub object, all I want DoInTransaction to do is run method() . Something like: // pseudo-code unitOfWorkStub.Stub(x => x.DoInTransaction(method)).Do(method()) Is it possible to create this kind of a stub with RhinoMocks? How can this be done? 回答1: use this: unitOfWorkStub.Stub(x => x.DoInTransaction(Arg<Action>.Is.Anything)) .WhenCalled(x => ((Action)x.Arguments[0])());

How to use the real parameters when creating a stub method in RhinoMocks?

时间秒杀一切 提交于 2021-02-06 11:24:42
问题 I want to create a stub of the following interface: interface IUnitOfWork { void DoInTransaction(Action method); } In the stub object, all I want DoInTransaction to do is run method() . Something like: // pseudo-code unitOfWorkStub.Stub(x => x.DoInTransaction(method)).Do(method()) Is it possible to create this kind of a stub with RhinoMocks? How can this be done? 回答1: use this: unitOfWorkStub.Stub(x => x.DoInTransaction(Arg<Action>.Is.Anything)) .WhenCalled(x => ((Action)x.Arguments[0])());

Mockito: How to easily stub a method without mocking all parameters

不问归期 提交于 2020-01-12 06:26:46
问题 I have a method i'd like to stub but it has a lot of parameters. How can i avoid mocking all parameters but still stub the method. Ex: //Method to stub public void myMethod(Bar bar, Foo foo, FooBar fooBar, BarFoo barFoo, .....endless list of parameters..); 回答1: I don't quite follow what problem you're having using Mockito. Assuming you create a mock of the interface that contains your myMethod() method, you can then verify only the parameters to the method that you are interested in. For

Mockito: How to easily stub a method without mocking all parameters

橙三吉。 提交于 2020-01-12 06:26:12
问题 I have a method i'd like to stub but it has a lot of parameters. How can i avoid mocking all parameters but still stub the method. Ex: //Method to stub public void myMethod(Bar bar, Foo foo, FooBar fooBar, BarFoo barFoo, .....endless list of parameters..); 回答1: I don't quite follow what problem you're having using Mockito. Assuming you create a mock of the interface that contains your myMethod() method, you can then verify only the parameters to the method that you are interested in. For

Mockito when/then not returning expected value

ⅰ亾dé卋堺 提交于 2019-12-29 02:08:34
问题 I'm trying to stub this getKeyFromStream method, using the 'any' matchers. I've tried being more explicit and less explicit (anyObject()), but it seems like no matter what I try, this stub will not return the fooKey in my unit test. I'm wondering if it is because it is protected or there is something else I'm missing or doing incorrectly. I have other when/then statements throughout the tests that are working but for some reason here, it is not. Note: The getKeyFromStream generally uses a

mocha and nested objects

谁说胖子不能爱 提交于 2019-12-24 12:50:39
问题 Excuse if this is a silly question, I am new to mocking. I am able to use mocha to do things like: person.expects(:first_name).returns('David') How can I mock a nested object? Say I have a Product that belongs to a Person and I want to get the first name of that person. In my app I might do it like this: product.person.first_name How would I get the same result using a mock? 回答1: as an alternative to shingara's answer, you could use mocha's any_instance method "which will detect calls to any

Should I mock local method invocation inside a tested method?

允我心安 提交于 2019-12-24 04:12:06
问题 My question about the concept of unit testing: class A { public void m1() { // code m2(); //code } public void m2() { //some code } } According to best practices, how should I test the m1 method? Is the unit the class or is the unit the method? My view - I should test m2 separately and I shouldn't test m1 and m2 integration. To use my view is hard enough - I should use sophisticated frameworks for testing and use very modern things. According to my sense of unit testing, tests should be

Should I mock local method invocation inside a tested method?

烈酒焚心 提交于 2019-12-24 04:11:45
问题 My question about the concept of unit testing: class A { public void m1() { // code m2(); //code } public void m2() { //some code } } According to best practices, how should I test the m1 method? Is the unit the class or is the unit the method? My view - I should test m2 separately and I shouldn't test m1 and m2 integration. To use my view is hard enough - I should use sophisticated frameworks for testing and use very modern things. According to my sense of unit testing, tests should be

Stubbing File.open with Rspec

青春壹個敷衍的年華 提交于 2019-12-23 17:53:05
问题 I'm attempting to stub File.open in order to test a method I have that reads a CSV file. Here's the model: class BatchTask def import(filename) CSV.read(filename, :row_sep => "\r", :col_sep => ",") end end Here's the spec code: let(:data) { "title\tsurname\tfirstname\rtitle2\tsurname2\tfirstname2\r"} let(:result) {[["title","surname","firstname"],["title2","surname2","firstname2"]] } it "should parse file contents and return a result" do File.stub(:open).with("file_name","rb") { StringIO.new