How to ensure views get updated while running tests

I'm using couchbase-model with Ruby 1.9.3, and I'm running tests within RSpec.

I've created a User model, and I have a method called "authorize" that I want to use to validate logins, with an email and password. I also have a view called "by_email", which returns a key of the email address. My intention is to query the by_email view with the provided email address as a key.

The problem I'm running into is that often the view, "by_email", is not updated. I have a few questions about this, however.

The code looks like this:

let( :user ) { FactoryGirl.build( :user ) }
before( :each ) do
user.save!
User.by_email( stale: false ).fetch_all
end

1) The only way I seem to be able to guarantee the view is updated is call #save! on user with observe: { persisted: 1 } as an option, and to query the by_email view with stale:false. If I eliminate the observe: { persisted: 1 } option, or switch stale to :update_after, it will inconsistently fail. I'd say 1 out of every 10 times it fails without the observe persisted option. Is this expected behavior?

2) Is this how you would recommend going about writing tests for this type of situation? If I have both options set, it dramatically slows down my tests. I understand that the observe option is not commonly needed for production environments, but what about test environment such as mine?

3) Is there any way I can ensure all views are always up to date for the purpose of the test database? The problem with query the User by_email view in my before block is that it requires that I know how the 'authorize' method works internally. I would rather not have my tests' success or failure depend on (brittle) test wrappers if possible.

4) What else can you tell me about testing? I'm trying to understand the best way to go about writing tests for my code with Couchbase as a backend.

Thanks.

1 Answer

« Back to question.

It is true that the only way to be totally sure, is to use "save with persistence guarantee" with "stale=false option to views"

I think you can try to use cbgb project for doing that. It is lightweight version of Couchbase written in Go language, which exposes the same API: http://cbgb.io/

By default cbgb will listen on the same port, so that you don't need change anything on the ruby side.