First test
assert_changes '#user_end.login_name' do
post update_login_user_path(id: #user_end.id), params: { user: { email: '', mobile_nation_id: 1, mobile: 876321 } }
end
"#user_end.login_name" didn't change.
Expected "end_consumer_email#mail.co" to not be equal to "end_consumer_email#mail.co".
Second test
post update_login_user_path(id: #user_end.id), params: { user: { email: '', mobile_nation_id: 1, mobile: 876321 } }
assert_equal(39876321, #user_end.login_name)
Expected: 39876321
Actual: "end_consumer_email#mail.co"
Both are returning the fixture value and not the expected value, which is generated in practice in the application. The controller action :
if params[:user][:email].present? || params[:user][:mobile].present?
set_user_login_name
end
calls a method
def set_user_login_name
if !params[:user][:email].blank?
params[:user][:login_name] = params[:user][:email].gsub(/\s+/, "")
elsif !params[:user][:mobile_nation_id].blank? && !params[:user][:mobile].blank?
#nation = Nation.where(id: params[:user][:mobile_nation_id]).first
params[:user][:login_name] = #nation.phone_cc.to_s + params[:user][:mobile].to_s
params[:user][:twilio_number] = '+' + #nation.phone_cc.to_s + params[:user][:mobile].to_s
else
params[:user][:login_name] = ''
end
params[:user][:kee] = SecureRandom.alphanumeric(32)
params[:user][:virtual_qr_code] = params[:user][:login_name] + params[:user][:kee]
end
As the methods work in practice, it is clear I do not grasp how to properly use these two assertions. I don not seem to be able to call the updated record (whereas for a create assert_equal('18831912200', User.last.login_name) is straightforward)
How can either one of them be cast to assert the above method?
Related
Need to write RSpec for specific function:
def display_type(record,house_record)
#user_value = either user will provide input through enviroment, else it will be ['dctEng']
user_value =
if ENV['USER_VALUE'].nil?
ServiceConfig[:USER_VALUE]
else
ENV['USER_VALUE'].split(',')
end
user_value.map!(&:downcase)
myArr = [
'ani',
'awe',
'emi'
]
if user_value.include?(record[:mnemonic].downcase)
return (myArr.include?(house_record[:name] || house_record[:mnemonic]) ? true : false)
else
return [true, false].sample
end
end
I tried this one:
I have added the variable record and house_record but don't know how to properly write testcase for this method
describe '#display_type' do
let(:record) do
{
name: 'test_name',
mnemonic: 'test_schema_name1'
}
end
let(:house_record) do
{
name: 'test_name',
mnemonic: 'test_mnemonic',
row: true,
col: true
}
end
let(:user_value) do [
'test_name1',
'test_name2'
] end
let(:myArr) do [
'test1',
'test2',
'test3',
'test4'
] end
#updated_code:
it 'should include record[:mnemonic] in myArr array' do
result = display_type(record,house_record)
expect([true]).to include(result)
end
it 'should not include record[:mnemonic] in myArr array' do
result = display_type(record,house_record2)
expect([true,false]).to include(result)
end
end
But don't know how to complete it, getting continuous error:
It is not coming under IF block return statement, I tried to check it using binding.pry, it is giving true for
if user_value.include?(record[:mnemonic].downcase)
return (myArr.include?(house_record[:name] || house_record[:mnemonic]) ? true : false)
But while running rspec it is going to else block and do sampling based on true and false
Kindly, give a look, I updated one case, how to improve it, because it is not going in if condition
I have the following:
myObject = {
id: user.id,
email: user.email,
}
I need to add values like so:
if current_user && current_user.id == user.id
myObject << {
notification_email: user.notification_email,
notification_email2: user.notification_email2
}
end
The code above raises an error.
What's the right way to optionally append values to the object?
ERROR
undefined method `<<' for # Did you mean? <
Perhaps you want Hash#merge.
myObject.merge(
notification_email: user.notification_email,
notification_email2: user.notification_email2
)
If you want side effects, use the banged version.
myObject.merge!(
notification_email: user.notification_email,
notification_email2: user.notification_email2
)
myObject is a hash, so to add new items you can do this:
if current_user && current_user.id == user.id
myObject[:notification_email] = user.notification_email
myObject[:notification_email2] = user.notification_email2
end
I have an rails application wherein i am scraping data from the internet. I have this snippet of code where it reports syntax errors thus preventing it from running.
I have tried to sort it out but unable to find out what is wrong. Where am i going wrong.
The snippet is shown below:
def reuters
ticker_sym = 'FB.O'
reuters_home_url = "http://in.reuters.com"
reuters_base_url = "http://in.reuters.com/finance/stocks/"
board_members = Nokogiri::HTML(open(reuters_base_url + 'companyOfficers?symbol=' + ticker_sym.to_s ))
members = []
table = board_members.css('.column1 tbody.dataSmall').first
table_desc = board_members.css('.column1 tbody.dataSmall')[1]
table.css('tr').each_with_index do |row,index|
next if index == 0
members << {
name: row.css('td[1] h2 a').text.strip,
title: row.css('td[4]').text.strip,
position_held: row.css('td[3]').text.strip,
age: row.css('td[2]').text.strip,
member_link: URI.join(reuters_home_url,row.css('td[1] h2 a').attr("href")).to_s
table_desc.css('tr').each_with_index do |col,index2|
next if index2 == 0
members << {
description: col.css('td[2]').text.strip
}
end
}
end
end
Have attached a screenshot of my rails application error page shown below:
Rails error page
Add } before table_desc.css('tr').each_with_index do |col, index2| and remove } after end like this.
def reuters
ticker_sym = 'FB.O'
reuters_home_url = "http://in.reuters.com"
reuters_base_url = "http://in.reuters.com/finance/stocks/"
board_members = Nokogiri::HTML(open(reuters_base_url + 'companyOfficers?symbol=' + ticker_sym.to_s))
members = []
table = board_members.css('.column1 tbody.dataSmall').first
table_desc = board_members.css('.column1 tbody.dataSmall')[1]
table.css('tr').each_with_index do |row, index|
next if index == 0
members << {
name: row.css('td[1] h2 a').text.strip,
title: row.css('td[4]').text.strip,
position_held: row.css('td[3]').text.strip,
age: row.css('td[2]').text.strip,
member_link: URI.join(reuters_home_url, row.css('td[1] h2 a').attr("href")).to_s
}
table_desc.css('tr').each_with_index do |col, index2|
next if index2 == 0
members << {
description: col.css('td[2]').text.strip
}
end
end
end
Is there a way to set conditional filters for contexts in rspec?
I have a context with variables that are only valid if a particular variable != 1, so I wanted to use that as the filter for that context's test. I can't have the filter on the test itself because I'm using said variables to set the context for that test (see below for ideal state):
context 'if interval is not 1 every non interval day', :if => Reminders::INTERVAL != 1 do
num = rand(0..100)
non_limbo = num + num/(Reminders::INTERVAL - 1) + 1
let!(:evaluation) { create(:evaluation, created_at: non_limbo.days.ago) }
it 'doesn't send an email' do
expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(0)
end
end
You can surround the whole context in a condition:
if Reminders::INTERVAL != 1
context 'if interval...' do
# ...
end
end
Suppose the model method foo() returns an array [true, false, 'unable to create widget']
Is there a way to write an rspec example that passes that array as a block that verifies [0] = true, [1] = false, and [2] matches a regex like /
Currently, I do it like:
result = p.foo
result[2].should match(/unable/i)
result[0].should == true
result[1].should == false
I can't quite get my head around how that might be doable with a block?
It would be slightly over engineered but try to run this spec with --format documentation. You will see a very nice specdocs for this method ;)
describe '#some_method' do
describe 'result' do
let(:result) { subject.some_method }
subject { result }
it { should be_an_instance_of(Array) }
describe 'first returned value' do
subject { result.first }
it { should be_false }
end
describe 'second returned value' do
subject { result.second }
it { should be_true }
end
describe 'third returned value' do
subject { result.third }
it { should == 'some value' }
end
end
end
Do you mean that your result is an array and you have to iterate over to test it's various cases?
Then, you can do that by following, right:
result = p.foo
result.each_with_index do |value, index|
case index
when 0 then value.should == true
when 1 then value.should == false
when 2 then value.shoud match(/unable/i)
end
end