How to type text in a rubymotion functional test? - rubymotion

What I would like is to have a something like this, but I couldn't find something like the *type_text* method in the docs:
describe "login controller" do
tests LoginController
it "lets you login" do
type_text('user#example.com', :into => 'Email address') # Made up method
type_text('mypassword', :into => 'Password') # Made up method
tap('Login')
view('Login succes!').should.not == nil
end
end
Any ideas?

That is a nice question. One would think that there is something in the toolkit to achieve this...
The only thing I know of would be a little detour. You select the view that stores the data like here: http://www.rubymotion.com/developer-center/articles/testing/#_finding_views
And then simply change the text property of the view.

Related

Overriding Devise::RegistratoinsController

So I am trying to override Devise::RegistrationsController which they do have wiki for and tons of tutorial out there. The one thing that I can not find is the best implementation of how to override the controller whilst implementing the require admin approval feature as well.
I think I got the hang of it but before I go any further (from all the reading on the Devise's source code) I want to know, on the registrations controller there's a line that does:
resource.active_for_authentication?
However, on the Sessions controller it's just this:
def create
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_flashing_format?
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
end
What I want to know is, if it's not confirmed or the active_for_authentication returns false, where or how does the session controller check this? I tried tracing back the source code but no luck.
So anyone who's very familiar with Devise perhaps you could answer my question? Thank you.
After authenticating a user and in each request, Devise checks if your model is active by calling model.active_for_authentication?. This method is overwritten by other devise modules. For instance, :confirmable overwrites .active_for_authentication? to only return true if your model was confirmed.
You can overwrite this method yourself, but if you do, don't forget to call super:
def active_for_authentication?
super && special_condition_is_valid?
end
Whenever active_for_authentication? returns false, Devise asks the reason why your model is inactive using the inactive_message method. You can overwrite it as well:
def inactive_message
special_condition_is_valid? ? super : :special_condition_is_not_valid
end

Fake select field for Simple Form

I'm using Simple Form, and I have a few fields that are not associated with my model. I found using this fake field option to be a good solution:
https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes
I thought this was cleaner than adding an attr_accessor value for my fields, and it works great for text input fields. I'm hoping to accomplish the same thing with a Select Field.
I found this thread, but I couldn't find anything else:
https://github.com/plataformatec/simple_form/issues/747
Has anyone found a similar option for a Fake Select Input? Thanks!
Assuming you'll use that "fake select" for UI purposes (probably as a mean to modify the form fields to present the user using Javascript?) and you don't want to deal with the value in the controller, you could just use select_tag with any field name, instead of the simple_form f.input. The value will be sent to the server, but it will be outside the model params, so you can just ignore it in the controller.
If I misunderstood your question, please clarify.
If your just trying to get the name='whatever' instead of having name='model[whatever]' I've found it easiest to just specify the name attribute in input_html { name: 'whatever', id: 'whatever' } hash which over rides the default model[attribute].
Otherwise you could create a fake_select_input.rb which would be similar to fake_input.rb but obviously use a select_tag instead and do something like as: :fake_select

Rails Validations - Reverse of Confirmation Validation

I want to be sure, two attributes don't have the same value with a validation in my Rails4 application. I know about confirmation validation but I need exactly the opposite of that.
Does Rails have this kind of validation?
You need to create a custom validation I think:
validate :check_attribute1_and_attribute2
def check_attribute1_and_attribute2
if attribute_1 == attribute_2
errors.add( :column_2, ' Value 2 cannot be similar to Value 1!')
end
end
Hope it helps :)
I had a similar need and wanted a simple solution. I thought this worked out pretty well in the end.
validates :applicant_id, exclusion: {
in: -> (reference_request) { [reference_request.reference_id] },
message: 'cannot also be a reference'
}

Understanding Rails/Twitter On-click tweet on behalf of user

As preface, I've followed through some tutorials (i.e. Michael Hartl's) though I'm still fairly novice. Forgive any cloudy terminology.
I am trying to build a simple application in Rails 4 that does the following:
User logs into application (currently working with sign-in-with-twitter link and routing)
get "/auth/:provider/callback" => "sessions#create"
get "/signout" => "sessions#destroy", :as => :signout
Once <% if current_user %> is true, I have the view rendering a partial where there will be a list of simple buttons. When the user clicks a button I want the application to tweet on behalf of the current_user a preset string. Ideally, I'd do this all in ruby/rails.
These button functions are where I'm getting hung up. I've read a fistful of documents but there seem to be a lot of conflicting and old answers. Here's a quick list of the ones I think are closest, though not explicit about sending a tweet from a simple button/link in a view:
http://www.sitepoint.com/ruby-social-gems-twitter/
http://richonrails.com/articles/sending-a-tweet-to-twitter
Some call for controllers, a more robust oauth setup (which I have bundle installed and connected to the dev.twitter application, though not fleshed out beyond keys), and whatever else. It's got me turned around and I'm not yet good enough to synthesize all the information. Any help and direction would be great. Below are some other files in the app that might be helpful.
class SessionsController < ApplicationController
def create
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url, :notice => "Hi!"
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Bye!"
end
end
And omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, '_priv', '_priv'
end
Eep! I'm the author of the second link (RichOnRails). Did you take a look at the example app included with the tutorial? It does almost exactly what you want. If the tweets are hard coded you could approach it in a couple of different ways. If you take a look at the tweets controller, you'll see it takes a parameter called 'message'. Any message passed to that create method will tweet as the current user.
def create
current_user.tweet(twitter_params[:message])
end
The easiest (but not necessarily best) way to adapt this to fit your needs is to have a form for each tweet, and do a hidden field with the message you wish to tweet. The button becomes a submit for that particular form (you can add remote: true if you want to keep the page from refreshing, then use a bit of javascript to update the UI elements). Hope this helps.

What assertions/tests are needed in a basic "Save/Submit" model test case?

Can anyone help me with what assertions are required in a basic save method test case in CakePHP 2.0?
I have Product, User and News model, I am looking to write a test case for the submit method in the News model and there are so many ways/things to include I was just wondering what is actually needed and what isn't. I have basic fixtures setup for all models.
The method I'm testing will effectively be this:
class News extends AppModel {
public submit($productId, $userId, $newsData) {
// Logic which checks for user and products existence, saves and returns submitted row
}
}
Test Case
public function testSubmit() {
// Save News
$newsData = array(
'News' => array(
'title' => 'Here is the title of the news',
'body' => 'Here is the news body',
'source' => 'News came from here'
)
);
$news = $this->News->submit('product-1', 'user-1', $newsData);
// Now what?
}
Simply assert that $news is an array, object, that the array is equal to an array you expect... Whatever your method returns, you should know it even before implementing the method (test driven development) and be able to assert the result using one or more than one of the phpunit assert methods.
Like simply $this->assertTrue($news); Check the manual for all the asserts. http://www.phpunit.de/manual/current/en/
Also look at the CakePHP core tests to get an idea of how to do test.
Or look at some open source plugin exmaples like
https://github.com/CakeDC/tags/blob/2.0/Test/Case/Model/TaggedTest.php
or
https://github.com/CakeDC/users/blob/2.0/Test/Case/Model/UserTest.php