I need to define a factory for a database table which contains column named 'method'.
I've tried something like this:
FactoryGirl.define do
factory :notification_baby_offset, class:'NotificationTemplate' do
...
method {{ 'email' => false, 'notification' => true }}
...
end
end
But, when I tried to build it, I got an error saying:
`method': wrong number of arguments (0 for 1) (ArgumentError)
It appears to identify 'method' as a function rather than a database column. I tried to emphasize this by :method or "method", but that didn't work.
So, I have to build this factory like this for it to work:
FactoryGirl.create(:notification_baby_offset, method: { 'email' => false, 'notification' => true })
Is where a way to avoid this hack and define this column properly in a factory?
Try this:
FactoryGirl.define do
factory :notification_baby_offset, class:'NotificationTemplate' do
...
after(:build) do |notification|
notification.method = { 'email' => false, 'notification' => true }
end
...
end
end
Or even (if your new supports passing attributes):
FactoryGirl.define do
factory :notification_baby_offset, class:'NotificationTemplate' do
...
initialize_with { new(method: { 'email' => false, 'notification' => true }) }
...
end
end
Related
I read previous possible questions that may have the answer but that not what I asked for.
First of all I am start to use test. However I already successful setup Omniauth-facebook for my App but still like to go back and test.
-sessions_controller.rb
class SessionsController < ApplicationController
def new
#title= 'Sign In'
end
def create
auth = request.env["omniauth.auth"]
user = User.from_omniauth(auth)
session[:user_id] = user.id
if params.permit[:remember_me]
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
refresh_to root_path, :ma_notice => "Logged in"
rescue
redirect_to root_path, :alert=> "Authentication failed, please try again."
end
def destroy
#session[:user_id] = nil
cookies.delete(:auth_token)
refresh_to root_path, :ma_notice => "Logged Out"
end
def failure
ma_log "Authentication failed, please try again."
redirect_to root_path, :alert=> "Authentication failed, please try again."
end
end
-app/models/user.rb
class User
....
....
def self.from_omniauth(auth)
where(auth.slice(:uid, :provider, :email)).first_or_create do |user|
case auth.provider
when 'identity'
identity = Identity.find auth.uid
user.code = identity.code
user.email = identity.email
else
user.email = auth.info.email
user.uid = auth.uid
user.provider = auth.provider
user.code = auth.info.name
user.role = "M"
end
end
end
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
So what I did
Test routes (Its seem simple but sometime I might forgot because I changing from dynamic route to fixed route as required in rails 5.2)
-test/integration/authen_test.rb
require 'test_helper'
class RoutesTest < ActionController::TestCase
test 'facebook login' do
assert_routing '/auth/facebook/callback', {controller: 'sessions', action: 'create',provider: 'facebook'}
end
test 'facebook login post' do
assert_routing({path: '/auth/facebook/callback', method: 'post'},{controller: 'sessions', action: 'create' ,provider: 'facebook'})
end
end
I want to test if facebook accept login and return call back.
-test/models/user_test.rb
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "Facebook validation" do
auth = {provider: :facebook, FACEBOOK_API: "111111111111111", FACEBOOK_KEY: "11111111111111111111111111111111"}
user = User.from_omniauth(auth)
puts user
assert_not nil
end
end
Problem: It's always green even change FACEBOOK_API. I found the user from puts as well. It's seem like user.from _omniauth already gone to facebook and get info using FACEBOOK_API from .env not one I provided. Then how I can test if it really connected with facebook.
The same with this related test. It's always green in any FACEBOOI_API. That won't work as well.
View Test. I like to test if no facebook login the system or not. The login would display accordingly. Still have no idea as stuck with Q.2 perhap someone could share how you do the test.
To Setup Integration Testing-Omniauth as documented
You can turn on "test mode" for OmniAuth like so:
OmniAuth.config.test_mode = true Once you have enabled test mode, all
requests to OmniAuth will be short circuited to use the mock
authentication hash as described below. A request to /auth/provider
will redirect immediately to /auth/provider/callback.
Then said
OmniAuth.config.add_mock(:twitter, {:uid => '12345'})
OK but where to put that code to turn on
so I assumed the only one is
-config/initializer/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :identity,
:fields => [:code, :email],
:on_failed_registration=> lambda { |env|
IdentitiesController.action(:new).call(env)
}
provider :facebook, ENV['FACEBOOK_API'], ENV['FACEBOOK_KEY']
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
end
Here I used test from related question but used my method :create.
It didn't do anything than green, even changed .test_mode = false
-test/integration/sessions_controller_test.rb
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
test '#signup_success' do
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
'provider' => 'facebook',
'uid' => '123451234512345',
'info' => {'email' => 'testuser#testmail.com', 'name' => 'test', 'image' => ''}
})
request.env['omniauth.env'] = OmniAuth.config.mock_auth[:facebook]
get :create
end
end
You can use omniauth test helpers. Here is the link:
https://github.com/omniauth/omniauth/wiki/Integration-Testing
Setup Mock Auth:
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
:provider => 'facebook',
:uid => '123545'
# etc.
})
What i did and what worked for me was setting up mocks in my spec_helper file for various scenarios.
OmniAuth.config.test_mode = true
omniauth_hash = { 'provider' => 'twitter',
'uid' => '12345',
'info' => {
'name' => 'test',
'email' => 'test#test.com',
'nickname' => 'testnick'
},
'extra' => {
'raw_info' =>
{
'location' => 'Coralvilleo'
}
}
}
omniauth_hash_fb = { 'provider' => 'facebook',
'uid' => '12345',
'info' => {
'name' => 'test',
'email' => 'test#testsomething.com'
},
'extra' => {'raw_info' =>
{ 'location' => 'Chicago'
}
}
}
omniauth_hash_fail = { 'provider' => 'facebook',
'uid' => '12345',
'info' => {
},
'extra' => {'raw_info' =>
{ 'location' => 'Chicago'
}
}
}
omniauth_hash_fail_2 = { 'provider' => 'facebook',
'uid' => '12345',
'info' => {
},
'extra' => {'raw_info' =>
{ 'location' => 'Chicago'
}
}
}
omniauth_hash_fail_complete = { 'provider' => 'twitter'}
OmniAuth.config.add_mock(:twitter, omniauth_hash)
OmniAuth.config.add_mock(:facebook, omniauth_hash_fb)
OmniAuth.config.add_mock(:facebook_fail, omniauth_hash_fail)
OmniAuth.config.add_mock(:twitter_fail, omniauth_hash_fail_2)
Then using these methods in my rspec tests for controller like so.
it 'should successfully create a user with twitter' do
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:twitter]
expect {
post :twitter, provider: :twitter
}.to change{ User.count }.by(1)
end
it 'should redirect the user to the root url with twitter' do
request.env['omniauth.auth'] = OmniAuth.config.mock_auth[:twitter]
post :twitter, provider: :twitter
response.should redirect_to root_path
end
name of the mocks to be specified and linked to what we specified in the helper.
OmniAuth.config.mock_auth[name of the mock you specified in spec helper].
Is there a way to pass a dynamic set of options to a textarea element in browser?
options = {
:type => 'textarea',
:selector => ':id',
:field => 'id_of_textarea_field',
:value => 'Joe Salesperson'
}
browser.textarea(options[:selector] => options[:field]).set ''
Error received:
invalid attribute: ":id"
A similar thread is listed here (selecting elements using variables in ruby /watir) but no answer.
options = {
:type => 'textarea',
:selector => :id,
:field => 'id_of_textarea_field',
:value => 'Joe Salesperson'
}
You can just pass the identifier, no need for quotes.
2017-01-03, Updating with a use case. Here is the declaration of the method I'm using and the call. The actual code is much more verbose and contains different validations than what I've pasted here. In the example, you'll notice a simple switch block which sets the appropriate information based on the element type passed (specifically text areas, text fields, and select elements).
def validateInput(options = {})
success = true
begin
case options[:type]
when 'textarea', 'text_field'
Watir::Wait.until{$browser.textarea(options[:selector] => options[:field]).present?}
$browser.textarea(options[:selector] => options[:field]).set options[:value]
when 'select'
$browser.select_list(options[:selector] => options[:field]).select_value options[:value]
else
puts "in else"
end
end
rescue => e
$message.push("Failed to validate '#{options[:field]}' field. #{e}")
success = false
end
return success
end
validateInput({
:type => 'textarea',
:selector => :id,
:field => 'order_approval_comment_name',
:value => 'Joe Salesperson'
})
I'm using the Axlsx gem (https://github.com/randym/axlsx) for generating excel files. I want to refactor my code so that I have a wrapper around the gem just in case I want to switch gems.
Going through this reminds me of the Adapter design pattern. However, there are just so many objects nested underneath the main Package object that I am getting confused as to how to actual create an adapter for it. Ex:
You create a Package object
You access the Workbook within this Package objet
You access the Sheet from the Workbook object, and vice versa
Here are some of my classes:
class ReportGenerator::Base
...
def create_workbook
...
#package = Axlsx::Package.new <---------------------------
#workbook = #package.workbook <---------------------------
#workbook.use_shared_strings = true
end
class Sheet::Base
def initialize(workbook, question, options = {})
...
#sheet = workbook.add_worksheet(:name => sheet_name) <---------------------------
end
def styles
#styles ||= {
"title" => #sheet.workbook.styles.add_style(:sz => 20, :b => true, :alignment => { :wrap_text => true }),
"bold" => #sheet.workbook.styles.add_style(:b => true),
"header" => #sheet.workbook.styles.add_style(:fg_color => "FFFFFF", :bg_color => "ff3333", :sz => 12, :b => true, :alignment => {:horizontal => :center}, :border => {:style => :thin, :color => "FFFFFF"}),
"subheader" => #sheet.workbook.styles.add_style(:fg_color => "FFFFFF", :bg_color => "ff3333", :sz => 12, :b => true, :alignment => {:horizontal => :center}),
"subheader_border_left" => #sheet.workbook.styles.add_style(:fg_color => "FFFFFF", :bg_color => "ff3333", :sz => 12, :b => true, :alignment => {:horizontal => :center}, :border => {:style => :thin, :color => "FFFFFF", :edges => [:left]}),
"blue_link" => #sheet.workbook.styles.add_style(:fg_color => '0000FF'),
"wrap_text" => #sheet.workbook.styles.add_style(:alignment => { :wrap_text => true, :horizontal => :left }),
"percentage" => #sheet.workbook.styles.add_style(:format_code => "0.00%")
}
end
Here's my first stab:
class ExcelWriter
def initialize
#package = Axlsx::Package.new
end
def workbook
#package.workbook
end
# starting to feel like it's not doable within one class..?
end
There are so many classes involved that it feels like I can't wrap everything into one adapter? Or maybe I'm doing it wrong? Any tips would be welcomed.
Focus on what you're actually using and not on existing infrastracture of Axlsx gem. This way you may combine work of several Axlsx objects into 1 method call.
I don't know what you're actually using, so it's hard for me to say which objects do you need.
In a cookbook I have the following in my attributes/default.rb:
default.ark.packages = [
{
'name' => 'optipng',
'url' => 'http://squirrelyjim.cloudfront.net/heroes/optipng-0.7.5.tar.gz',
'version' => '0.7.5'
},
{
'name' => 'imagemagick',
'url' => 'http://squirrelyjim.cloudfront.net/heroes/ImageMagick-6.9.0-4.tar.gz',
'version' => '6.9.0-4'
},
{
'name' => 'jpegoptim',
'url' => 'http://squirrelyjim.cloudfront.net/heroes/jpegoptim-1.4.1.tar.gz',
'version' => '1.4.1'
}
]
I then call those values using the ark resource as follows:
node.ark.packages.each do |pkg|
ark pkg['name'] do
url pkg['url']
version pkg['version']
action :install_with_make
notifies :run, "execute[ldconfig]", :immediately
end
end
This works great but I would like to somehow get the version to automatically get called at the end of the url, instead of typing it out twice. Is there a way to get a value in a hash updated with another value from the same hash, similar to:
http://squirrelyjim.cloudfront.net/heroes/optipng-#{version}.tar.gz
Dynamically build the URL inside the loop:
node.ark.packages.each do |pkg|
url = "http://squirrelyjim.cloudfront.net/heroes/#{pkg['name']}-#{pkg['version']}.tar.gz"
ark pkg['name'] do
url url
version pkg['version']
action :install_with_make
notifies :run, "execute[ldconfig]", :immediately
end
end
I'm developing an small app using Sinatra. So far so good but I'm having a really small problem and I don't understand why this is happening.
I have a class Note with a custom to_json:
class Note < ActiveRecord::Base
#id
#name
#body
#created_at
#updated_at
def to_json(options={})
{ 'id' => self.id,
'name' => self.name,
'body' => self.body,
}.to_json
end
end
If I call:
Note.first.to_json
It returns:
=> "{\"id\":1,\"name\":\"this is the name\",\"body\":\"this is the body\"}"
If I add the object inside an array and call to_json of that array
array = Array.new
array.push Note.first
array.to_json
It returns:
=> "[{\"id\":1,\"name\":\"this is the name\",\"body\":\"this is the body\",\"updated_at\":\"2014-01-17T22:00:45-03:00\",\"created_at\":\"2013-04-17T21:21:20-03:00\"}]"
So the to_json from the class Note is not getting called because I still get the updated_at and the created_at
What am I doing wrong? (btw, I'm using the json gem)
Thanks.
Rename your to_json method to as_json, remove .to_json on a hash and it should work.
def as_json(options={})
{
'id' => self.id,
'name' => self.name,
'body' => self.body,
}
end
You can serialize the record before adding it to the array:
array = []
array << Note.first.to_json
array.to_json