What’s the best way to unit test protected & private methods in Ruby?

You can bypass encapsulation with the send method: myobject.send(:method_name, args) This is a ‘feature’ of Ruby. 🙂 There was internal debate during Ruby 1.9 development which considered having send respect privacy and send! ignore it, but in the end nothing changed in Ruby 1.9. Ignore the comments below discussing send! and breaking things.

What is a good example to differentiate between fileprivate and private in Swift3

fileprivate is now what private used to be in earlier Swift releases: accessible from the same source file. A declaration marked as private can now only be accessed within the lexical scope it is declared in. So private is more restrictive than fileprivate. As of Swift 4, private declarations inside a type are accessible to … Read more

Unit testing of private functions with Mocha and Node.js

Check out the rewire module. It allows you to get (and manipulate) private variables and functions within a module. So in your case the usage would be something like: var rewire = require(‘rewire’), foobar = rewire(‘./foobar’); // Bring your module in with rewire describe(“private_foobar1”, function() { // Use the special ‘__get__’ accessor to get your … Read more

Any way to Invoke a private method?

You can invoke private method with reflection. Modifying the last bit of the posted code: Method method = object.getClass().getDeclaredMethod(methodName); method.setAccessible(true); Object r = method.invoke(object); There are a couple of caveats. First, getDeclaredMethod will only find method declared in the current Class, not inherited from supertypes. So, traverse up the concrete class hierarchy if necessary. Second, … Read more

static constructors in C++? I need to initialize private static objects

To get the equivalent of a static constructor, you need to write a separate ordinary class to hold the static data and then make a static instance of that ordinary class. class StaticStuff { std::vector<char> letters_; public: StaticStuff() { for (char c=”a”; c <= ‘z’; c++) letters_.push_back(c); } // provide some way to get at … Read more

Why can outer Java classes access inner class private members?

The inner class is just a way to cleanly separate some functionality that really belongs to the original outer class. They are intended to be used when you have 2 requirements: Some piece of functionality in your outer class would be most clear if it was implemented in a separate class. Even though it’s in … Read more

Can a constructor in Java be private?

Yes, a constructor can be private. There are different uses of this. One such use is for the singleton design anti-pattern, which I would advise against you using. Another, more legitimate use, is in delegating constructors; you can have one constructor that takes lots of different options that is really an implementation detail, so you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)