restrict rspec test case to development environment/database - mysql

I am having issues writing a test case for one of my controller actions which uses MySQL syntax. This is because my test environment database is sqlite3, so the syntax is not recognized.
I was looking for a way to customize which environment my test case runs in, and I found this answer, which I tried here:
Rspec.describe MyController, type :controller do
before do
allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("development"))
end
... my actual tests ...
but, it didn't work; I am still getting a
SQLite3::SQLException: no such function
Any suggestions?

Related

Django 4: settings.DEBUG not correct when running tests

I am trying to write a Django (4) test. It has to work differently depending on the DEBUG constant in my settings.py.
So I started with something like:
def my_test(self):
from django.conf import settings
if settings.DEBUG:
....
else:
....
I noticed the code never executed the first part of the if so I added:
print(settings.DEBUG)
And I noticed this prints always False. Even if DEBUG is set to True in my settings.py.
I tried with some others constants there (INSTALLED_APPS, ALLOWED_HOSTS) and all of them return the correct value.
Why is that? How to access the real DEBUG constant in my settings.py?
Adding --debug-mode flag solved the issue:
python manage.py test --debug-mode
--debug-mode
Sets the DEBUG setting to True prior to running tests. This may help troubleshoot test failures.

Skip/Mock Redis In Junit

I want to test a service which in turns create connection with redis.
I want to skip this part in my junit. Is there a way to skip this method call or mock it?
I think the question was more about how the Redis part can be mocked so that the test run when redis isn't available. It's hard because your service is probably using the connection so you'd have to do a lot of mocking. What we do in Spring Boot is check if a redis server is available on localhost and if that's the case run the tests, otherwise skip.
See RedisTestServer and a sample usage. Note that the rule applies to all the tests so you may want to move the tests that are using Redis in an isolated test class.
Annotate with #Ignore to ignore the method. Like this:
#Ignore("reason of skipping")
#Test
public void testConnectionCreation(){
// do some stuff...
}
Optionally you can provide a note to why the test is ignored as shown above.
See http://junit.sourceforge.net/javadoc/org/junit/Ignore.html for more info.

Hudson build - MStest to not consider Inconclusive test as failure

We are having couple of test cases marked as inconclusive for maintenance, issue is with our Hudson build which is considering Inconclusive test cases as Error.
We have enabled failonerror = "true" in build xml. Guess MsTest is making decision on error status and not Hudson.
is there any command line argument to ignore Inconclusive test as error.
Thanks.
MSTest reports Inconclusive as separate from failure, but returns a execution result of 1 if any tests are inconclusive (unlike NUnit, which does not). The build will interpret the 1 result code as a failure.
There is no command line option to turn this off (see http://msdn.microsoft.com/en-us/library/ms182489.aspx )
It may be possible to turn off the failonerror flag, and add a build step to parse for errors, but if you wish to turn off a test for maintenance, it would be better to use an [Ignore] attribute, like this:
[TestMethod, Ignore]
public void my_test () { ... }
Unlike NUnit, you can't add a reason for the ignore, so best leave a comment.

__persistence__ NoMethodError

I'm mocking a java interface in rspec
clock = ClockInterface.new
clock.should_receive(:currentTime)
When I run rspec everything works fine but I see a warning which directs me to the following
https://github.com/jruby/jruby/wiki/Persistence
When I attempt to set
ClockInterface.__persistence__ = true
I get a NoMethodError. I'm using jruby 1.7.4
ClockInterface is an interface rather than a class, and doesn’t have the __persistent__ method, unlike classes for which that method gets added through their proxy.
To get your test to work properly, you should instead use:
clock = mock(ClockInterface)
clock.should_receive(:currentTime)

spock: need a hook to perform some setup steps before any test class executes

I have several Spock test classes grouped together in a package. I am using Junit 4.10. Each test class contains several feature test methods.
I want to perform some setup steps (such as loading data into a DB, starting up a web server) before I run any test case, but only once when the testing starts.
I want this "OneTimeSetup" method to be called only once whether:
I run all the test classes in the package (for example if they are grouped in a Test Suite)
I run a few test classes
I run only one test class
I run only a certain feature method within a test class
From reading other posts on SO, it seems that this is what TestNG's #BeforeSuite does.
I am aware of Spock's setupSpec() and cleanupSpec() methods, but they only work within a given test class. I am looking to do something like "setupTestSuite()." How can this be achieved in Spock?
You can write a global extension, use a JUnit test suite, call a static method in a helper class (say from setupSpec) that does its work just once, or let the build tool do the job.