Rails 4, RSpec 3.2 - how to mockup ActionMailer's deliver_now method to raise exceptions - actionmailer

Environment
Ruby 2.2.1, Rails 4.2.0, rspec-core 3.2.2, rspec-expectations 3.2.0, rspec-mocks 3.2.1, rspec-rails 3.2.1, rspec-support 3.2.2
I have the following method
def send_event_alert_email(event_id)
event = Event.find_by(id: event_id)
...
...
...
EventAlertMailer.event_alert(event_id).deliver_now
create_alert(event)
end
I need to write specs which makes sure that create_alert(event) doesn't get invoked when EventAlertMailer.event_alert(event_id).deliver_now raises any exceptions. So the basic question is how do I simulate deliver_now to raise possible exceptions which it might actually throw.

Here is how to test deliver_now method:
describe EventAlertMailer do
it 'sends email' do
delivery = double
expect(delivery).to receive(:deliver_now).with(no_args)
expect(EventAlertMailer).to receive(:event_alert)
.with(event_id)
.and_return(delivery)
MyClass.send_event_alert_email
end
end

Related

How to workaround async incompatibilities when using mongodb-1.2.2 and rocket-0.5.0-rc.1?

I am working on building a rocket REST API using a mongodb database. I have been able to successfully create a connection and start up the server without errors:
# Cargo.toml
[dependencies]
rocket = "0.5.0-rc.1"
dotenv = "0.15.0"
mongodb = "1.2.2"
[dependencies.openssl]
version = "0.10"
features = ["vendored"]
#[get("/")]
pub async fn index(client: &State<ClientPointer>) -> &'static str {
let _dbs = client.0.list_databases(None, None).await.unwrap();
"Fetched databases"
}
#[launch]
async fn rocket() -> _ {
let client = database::connect::pool(1, 32).await.unwrap();
rocket::build()
.mount("/", routes!(routes::index))
.manage(database::rocket::ClientPointer(client))
}
However, when the route is invoked I get the following output:
>> Matched: (index) GET /
thread 'rocket-worker-thread' panicked at 'there is no timer running, must be called from the context of a Tokio 0.2.x runtime', C:\Users\lukasdiegelmann\.cargo\registry\src\github.com-1ecc6299db9ec823\tokio-0.2.25\src\time\driver\handle.rs:24:32
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
>> Handler index panicked.
>> This is an application bug.
>> A panic in Rust must be treated as an exceptional event.
>> Panicking is not a suitable error handling mechanism.
>> Unwinding, the result of a panic, is an expensive operation.
>> Panics will severely degrade application performance.
>> Instead of panicking, return `Option` and/or `Result`.
>> Values of either type can be returned directly from handlers.
>> A panic is treated as an internal server error.
>> Outcome: Failure
>> No 500 catcher registered. Using Rocket default.
>> Response succeeded.
So it seems like there is something wrong with the versioning of the used async runtime. But I could not find where, because the error do not really give me a hint and the mongodb rust driver appears to be using a 0.2.x version of tokio namely version ~0.2.18.
Edit: I have gained a little bit more insight and it seems like rocket-0.5.0-rc.1 has started using a tokio-1.x runtime, whereas mongodb-1.2.2 has not. This obviously imposes a big problem, since I will either have to have two runtimes run simultaneously or have to ditch mongodb for now, which is not exactly the definition of a solution.
MongoDB has released a 2.0.0-beta version of its driver that uses tokio-1.x.

getSchema in PostgreSQL JDBC driver throws java.lang.AbstractMethodError or java.sql.SQLFeatureNotSupportedException

I'm using Postgresql 8.4 and my application is trying to connect to the database.
I've registered the driver:
DriverManager.registerDriver(new org.postgresql.Driver());
and then trying the connection:
db = DriverManager.getConnection(database_url);
(btw, my jdbc string is something like: jdbc:postgresql://localhost:5432/myschema?user=myuser&password=mypassword)
I've tried various version of the jdbc driver and getting two type of errors:
with jdbc3:
Exception in thread "main" java.lang.AbstractMethodError: org.postgresql.jdbc3.Jdbc3Connection.getSchema()Ljava/lang/String;
with jdbc4:
java.sql.SQLFeatureNotSupportedException: Il metodo ½org.postgresql.jdbc4.Jdbc4Connection.getSchema()╗ non Þ stato ancora implementato.
that means: method org.postgresql.jdbc4.Jdbc4Connection.getSchema() not implemented yet.
I'm missing something but I don't know what..
------ SOLVED ---------
The problem were not in the connection String or the Driver version, the problem were in the code directly above the getConnection() method:
db = DriverManager.getConnection(database_url);
LOGGER.info("Connected to : " + db.getCatalog() + " - " + db.getSchema());
It seems postgresql driver doesn't have getSchema method, as the java console were often trying to say to me..
The Connection.getSchema() version was added in Java 7 / JDBC 4.1. This means that it is not necessarily available in a JDBC 3 or 4 driver (although if an implementation exists, it will get called).
If you use a JDBC 3 (Java 4/5) driver or a JDBC 4 (Java 6) driver in Java 7 or higher it is entirely possible that you receive a java.lang.AbstractMethodError when calling getSchema if it does not exist in the implementation. Java provides a form of forward compatibility for classes implementing an interface.
If new methods are added to an interface, classes that do not have these methods and were - for example - compiled against an older version of the interface, can still be loaded and used provided the new methods are not called. Missing methods will be stubbed by code that simply throws an AbstractMethodError. On the other hand: if a method getSchema had been implemented and the signature was compatible that method would now be accessible through the interface, even though the method did not exist in the interface at compile time.
In March 2011, the driver was updated so it could be compiled on Java 7 (JDBC 4.1), this happened by stubbing the new JDBC 4.1 methods with an implementation that throws a java.sql.SQLFeatureNotSupportedException, including the implementation of Connection.getSchema. This code is still in the current PostgreSQL JDBC driver version 9.3-1102. Technically a JDBC-compliant driver is not allowed to throw SQLFeatureNotSupportedException unless the API documentation or JDBC specification explicitly allows it (which it doesn't for getSchema).
However the current code on github does provide an implementation since April this year. You might want to consider compiling your own version, or ask on the pgsql-jdbc mailinglist if there are recent snapshots available (the snapshots link on http://jdbc.postgresql.org/ shows rather old versions).

Error in implementing hmsonline Cassandra-Trigger

Was trying to implement the triggers in Cassandra.
Have been trying to use the available Cassandra help: https://github.com/hmsonline/cassandra-triggers
Porting it on the latest version 1.2.3, and following the GettingStarted instruction.
when i tried to set the value for the trigger, inserting the data to fire the log, and checked the logs, following is the error which i received
java.lang.AssertionError
at org.apache.cassandra.thrift.ThriftSessionManager.currentSession(ThriftSessionManager.java:51)
at org.apache.cassandra.thrift.CassandraServer.state(CassandraServer.java:88)
at org.apache.cassandra.thrift.CassandraServer.validateLogin(CassandraServer.java:881)
at org.apache.cassandra.thrift.CassandraServer.set_keyspace(CassandraServer.java:1492)
at com.hmsonline.cassandra.triggers.dao.CassandraStore.getConnection(CassandraStore.java:42)
at com.hmsonline.cassandra.triggers.dao.ConfigurationStore.getConfiguration(ConfigurationStore.java:76)
at com.hmsonline.cassandra.triggers.dao.ConfigurationStore.isCommitLogEnabled(ConfigurationStore.java:44)
at com.hmsonline.cassandra.triggers.TriggerTask.run(TriggerTask.java:47)
at java.lang.Thread.run(Thread.java:636)
But we test it works fine at older version(ex: 1.1.2)
So is this a configuration problem, or the Thrift API implementation has been changed?
thanks

Undefined method `FactoryGirl' -- upgrading from 2.0.2 to 3.4.2

I'm in the process of upgrading from factory_girl (2.0.2 to 3.4.2) and factory_girl_rails (1.1.0 -> 3.4.0)
and I'm having issues with my rspec tests seeing factory girl.
I think I've successfully altered my factories to deal with the new syntax, and have removed the extra require statements that were bringing in multiple copies of the same files. My server now starts up, so I know that the factories.rb file is correctly getting parsed.
Now when I run my rspec tests, I'm getting this error:
NoMethodError: undefined method `FactoryGirl' for #
it 'can be created' do
course = FactoryGirl(:course)
….
end
With Factory Girl 3.4.2, you will need to explicitly use the create method.
course = FactoryGirl.create(:course)

Scala^Z3 kills running Thread on parser error

When I use Scala^Z3 (Z3 3.2 and according Scala^Z3 java library) and get a parser error like:
(error "line 21 column 41: invalid command, '(' expected")
Error: parser error
The executed Thread is killed and I cannot stop this by surrounding
the code with try/catch or anything.
Is there any way to stop this behaviour?
I am afraid there is not much to do: there is supposedly a call to exit in the Z3 library, and that is what causes this behavior.
As far as I understand the Java Native Interface, I cannot prevent the native function from terminating the process. The best I could do is add an exit hook to the JVM that could warn the user that the program is terminating due to external reasons, but that won't let you resume where you were in the control flow.
The ideal solution is of course that Z3 is updated so that no function in the public interface ever calls exit.