stubbing

Stubbing window.location.href with Sinon

折月煮酒 提交于 2019-11-30 22:29:37
问题 I am trying to test some client-side code and for that I need to stub the value of window.location.href property using Mocha/Sinon. What I have tried so far (using this example): describe('Logger', () => { it('should compose a Log', () => { var stub = sinon.stub(window.location, 'href', 'http://www.foo.com'); }); }); The runner displays the following error: TypeError: Custom stub should be a function or a property descriptor Changing the test code to: describe('Logger', () => { it('should

When to use stubs and mocks?

佐手、 提交于 2019-11-30 15:04:27
问题 I've this confusion all the time. If I write a code which uses fake code to assert some operation, how do i trust my real implementation when it is started really using the real objects instead of fake ones. For example, I've this code -- [Test] public void CanCreateContactsWithData() { using(ISession session = factory.OpenSession()) using (ITransaction trans = session.BeginTransaction()) { _contactId = (long) session.Save(contact); trans.Commit(); } Assert.AreNotEqual(0, _contactId); } This

When to use stubs and mocks?

拥有回忆 提交于 2019-11-30 13:31:47
I've this confusion all the time. If I write a code which uses fake code to assert some operation, how do i trust my real implementation when it is started really using the real objects instead of fake ones. For example, I've this code -- [Test] public void CanCreateContactsWithData() { using(ISession session = factory.OpenSession()) using (ITransaction trans = session.BeginTransaction()) { _contactId = (long) session.Save(contact); trans.Commit(); } Assert.AreNotEqual(0, _contactId); } This code tests the implementation of a "contact" object whether that gets saved into database or not. If i

Rhino Mocks stubs and mocks are only good for interfaces?

心已入冬 提交于 2019-11-30 11:02:10
Is it correct that Rhino Mocks stubs and mocks are only good for interfaces, not concrete classes? I spent quite a time trying to make this piece of code working. I did not expect the stubbed pubSubClient to always call Send method from the class. That method has some dependencies and throws exception. [Test] public void Test01() { PubSubMessage psm = new PubSubMessage(); var pubSubClient = MockRepository.GenerateStub<PubSubClient>(); pubSubClient.Stub(x => x.Send(psm)).IgnoreArguments().Return(null); // actual PubSubClient Send method throws exception // the rest of the test is skipped... }

Rhino Mocks stubs and mocks are only good for interfaces?

不想你离开。 提交于 2019-11-29 16:26:55
问题 Is it correct that Rhino Mocks stubs and mocks are only good for interfaces, not concrete classes? I spent quite a time trying to make this piece of code working. I did not expect the stubbed pubSubClient to always call Send method from the class. That method has some dependencies and throws exception. [Test] public void Test01() { PubSubMessage psm = new PubSubMessage(); var pubSubClient = MockRepository.GenerateStub<PubSubClient>(); pubSubClient.Stub(x => x.Send(psm)).IgnoreArguments()

Cleaning up sinon stubs easily

久未见 提交于 2019-11-28 03:25:11
Is there a way to easily reset all sinon spys mocks and stubs that will work cleanly with mocha's beforeEach blocks. I see sandboxing is an option but I do not see how you can use a sandbox for this beforeEach -> sinon.stub some, 'method' sinon.stub some, 'mother' afterEach -> # I want to avoid these lines some.method.restore() some.other.restore() it 'should call a some method and not other', -> some.method() assert.called some.method keithjgrant Sinon provides this functionality through the use of Sandboxes , which can be used a couple ways: // manually create and restore the sandbox var

Mockito - NullpointerException when stubbing Method

馋奶兔 提交于 2019-11-27 05:11:30
问题 So I started writing tests for our Java-Spring-project. What I use is JUnit and Mockito. It's said, that when I use the when()...thenReturn() option I can mock services, without simulating them or so. So what I want to do is, to set: when(classIwantToTest.object.get().methodWhichReturnsAList(input))thenReturn(ListcreatedInsideTheTestClass) But no matter which when-clause I do, I always get a NullpointerException, which of course makes sense, because input is null. Also when I try to mock

Stubbing authentication in request spec

安稳与你 提交于 2019-11-26 18:18:26
When writing a request spec, how do you set sessions and/or stub controller methods? I'm trying to stub out authentication in my integration tests - rspec/requests Here's an example of a test require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/authentication_helpers' describe "Messages" do include AuthenticationHelpers describe "GET admin/messages" do before(:each) do @current_user = Factory :super_admin login(@current_user) end it "displays received messages" do sender = Factory :jonas direct_message = Message.new(:sender_id => sender.id, :subject => "Message