Rspec Instance Double Received Unexpected Message - ruby

I'm writing rspec tests for my #check? method within my chess game but hitting an error with the start_coordinates variable stored in my Board class, which in rspec is an instance double.
Unsure how to allow my #check? method to change start_coordinates in rspec when it runs.
def check?(king_coordinates)
check = nil
#chess.board.each do |array|
array.each do |square|
next if square == '' || square.which_player != whos_turn
if square.which_player == whos_turn
row = #chess.board.detect{ |aa| aa.include?(square) }
#chess.start_coordinates = [row.index(square) + 1, 8 - #chess.board.index(row)]
check = square.valid_move?(#chess, square.which_player) ? true : false
else
false
end
end
end
check
end
My rspec looks like this:
describe '#check?' do
subject(:game_check) { described_class.new(player_one, player_two, board_class)}
let(:board_class) { instance_double(Board, board: [
['', '', rook, '', '', '', king, ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', bishop, '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '']
], start_coordinates: nil, finish_coordinates: nil) }
before do
allow(game_check).to receive(:whos_turn).and_return(player_one)
## allow(board_class).to receive(:start_coordinates).and_return([3, 8]) ## Tried this
end
it 'returns true' do
expect(game_check.check?([7, 8])).to eq(true)
end
end
My error message is:
#<InstanceDouble(Board) (anonymous)> received unexpected message :start_coordinates= with ([3, 8])
I have tried shown in the code above:
allow(board_class).to receive(:start_coordinates).and_return([3, 8])
Update to show Board class:
lass Board
attr_reader :board, :start_coordinates, :start_square, :finish_coordinates, :finish_square
def initialize(player1, player2)
#player1 = player1
#player2 = player2
#start_square = nil
#finish_square = nil
#start_coordinates = []
#finish_coordinates = []
#board = [
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', '']
]
end

You are calling the "setter" method start_coordinates= but you are only "allowing" the "getter" method start_coordinates.
Instead try changing
allow(board_class).to receive(:start_coordinates).and_return([3, 8])
To
allow(board_class).to receive(:start_coordinates=)
That being said a setter (method ending in =) will always return the argument passed in so and_return is not useful here but you could use with() if you want to be explicit about the arguments :start_coordinates= are expected to receive
Example:
def hello=(n)
'Hi'
end
hello= 12
#=> 12

Related

Unicode characters turn into question marks in Codeignter

I have a data with VARCHAR2 data type in database(oracle) like this "Mary’s" but when I try to retrieve it using codeigniter it displays "Mary?s" It replaces the single quote with question mark(?). I'm also getting the same result with Chinese characters
The character set of my database is AL32UTF8.
I'm using codeigniter and odbc driver
Please help me to fix this problem
I already added this code in my view:
<?php header('Content-Type: text/html; charset=utf-8');?>
Here's my database configuration:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => "test",
'hostname' => "",
'username' => 'test'
'password' => 'test123',
'database' => 'myShop',
'dbdriver' => 'odbc',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf-8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,

A Database Error Occurred - codeigniter

I got this error :
Unable to connect to your database server using the provided settings.
Filename: C:/xampp/htdocs/soheil/system/database/DB_driver.php
Line Number: 436
but when I add below code inside database.php it prints ok :
echo '<pre>';
print_r($db['default']);
echo '</pre>';
echo 'Connecting to database: ' .$db['default']['database'];
$dbh=mysql_connect
(
$db['default']['hostname'],
$db['default']['username'],
$db['default']['password'])
or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db['default']['database']);
echo '<br /> Connected OK:' ;
die( 'file: ' .__FILE__ . ' Line: ' .__LINE__);
my config :
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => 'soheil',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Are you put your username on database? It maybe root user
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'soheil',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Hope this help.

Custom product attribute not showing in admin catalog section magento 2.1

I have added custom product attribute in Magento 2.1 and that product is showing in attribute section but couldn't be shown in magento catalog section where we have created products
Below are the code which I am using to create attribute.
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'test_author',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Test Author',
'input' => '',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0,
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
You can try the following code -
/** #var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'test_author',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Test Author',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
]
);

A Database Error Occurred Error Number: It doesn't give a error number

This is the error:
A Database Error Occurred
Error Number:
select a.FOLIO, a.direccion, b.DESC_COLONIA, C.DESC_MPIO
from emergencias#db1 a INNER JOIN AMSGEOCODING#db1 Z ON A.FOLIO=Z.FOLIO inner join cat_colonia#db1 b on A.CVE_COL=b.cve_col and A.CVE_MPIO=b.cve_mpio and A.CVE_ent=b.cve_ent INNER JOIN CAT_MPIO#db1 C ON C.CVE_MPIO =A.CVE_MPIO AND C.CVE_ENT=A.CVE_ENT WHERE a.folio>=8782583 and z.LATITUD =0 and z.LONGITUD<>-1
Filename: C:\wamp\www\myapp\system\database\DB_driver.php
Line Number: 331
What could be wrong with this?
this is my C:\wamp\www\myapp\application\config\database.php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost/orcl',
'username' => 'local',
'password' => 'password',
'database' => 'orcl',
'dbdriver' => 'oci8',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

The problem of the relationship in Mysql, doctrine gets this error: Doctrine_Table_Exception: Unknown relation alias Model_Training

I have a problem with relationships in the Doctrine. Works on Mysql database. The problem occurs when downloading data using the relationship throws me this error: Doctrine_Table_Exception: Unknown relation alias Model_Training
Please help me get tired of it some days I sit on it and nothing came up, and google also did not find anything.
Here is the code that retrieves data:
$dataRows = Doctrine_Query::create()
->from('Model_Training a')->where('a.idTrainings = ?', $this->_getParam('id'))
->leftJoin('a.Model_TrainingBonuse ac')
->leftJoin('a.Model_Bonuse ad')
->fetchArray();
Schema.yml file with the formation of models for the Doctrine:
TrainingBonuse:
tableName: Trainings_has_Bonuses
columns:
Trainings_idTrainings:
type: integer(4)
notnull: true
autoincrement: true
Bonuses_idBonuses:
type: integer(4)
notnull: true
relations:
TrainingsIdTraining:
class: Training
local: Trainings_idTrainings
foreign: idTrainings
foreignAlias: Trainings_has_Bonuses
BonusesIdBonuse:
class: Bonuse
local: Bonuses_idBonuses
foreign: idBonuses
foreignAlias: Trainings_has_Bonuses
indexes:
fk_Trainings_has_Bonuses_Bonuses1:
fields: [Bonuses_idBonuses]
fk_Trainings_has_Bonuses_Trainings1:
fields: [Trainings_idTrainings]
options:
charset: utf8
collate: utf8_polish_ci
Bonuse:
tableName: Bonuses
columns:
idBonuses:
type: integer(4)
primary: true
notnull: true
autoincrement: true
name:
type: string(45)
title:
type: string(255)
description:
type: clob(65535)
file_url_full:
type: string(255)
file_type:
type: integer(1)
width_full:
type: integer(4)
height_full:
type: integer(4)
active:
type: integer(1)
default: 1
options:
charset: utf8
collate: utf8_polish_ci
Training:
tableName: Trainings
columns:
idTrainings:
type: integer(4)
primary: true
notnull: true
autoincrement: true
title:
type: string(255)
sub_title:
type: string(255)
up_body:
type: clob(65535)
training_body:
type: clob(65535)
question:
type: clob(65535)
opinion:
type: clob(65535)
bonuses:
type: integer(1)
notnull: true
default: 0
down_body:
type: clob(65535)
modyfication:
type: integer(1)
notnull: true
default: 0
active:
type: integer(1)
notnull: true
default: 1
options:
charset: utf8
collate: utf8_polish_ci
Models:
TRAINING
abstract class Model_Base_Training extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('Trainings');
$this->hasColumn('idTrainings', 'integer', 4, array(
'type' => 'integer',
'primary' => true,
'autoincrement' => true,
'length' => '4',
));
$this->hasColumn('title', 'string', 255, array(
'type' => 'string',
'length' => '255',
));
$this->hasColumn('sub_title', 'string', 255, array(
'type' => 'string',
'length' => '255',
));
$this->hasColumn('up_body', 'clob', 65535, array(
'type' => 'clob',
'length' => '65535',
));
$this->hasColumn('training_body', 'clob', 65535, array(
'type' => 'clob',
'length' => '65535',
));
$this->hasColumn('question', 'clob', 65535, array(
'type' => 'clob',
'length' => '65535',
));
$this->hasColumn('opinion', 'clob', 65535, array(
'type' => 'clob',
'length' => '65535',
));
$this->hasColumn('bonuses', 'integer', 1, array(
'type' => 'integer',
'notnull' => true,
'default' => 0,
'length' => '1',
));
$this->hasColumn('down_body', 'clob', 65535, array(
'type' => 'clob',
'length' => '65535',
));
$this->hasColumn('modyfication', 'integer', 1, array(
'type' => 'integer',
'notnull' => true,
'default' => 0,
'length' => '1',
));
$this->hasColumn('active', 'integer', 1, array(
'type' => 'integer',
'notnull' => true,
'default' => 1,
'length' => '1',
));
$this->option('collate', 'utf8_polish_ci');
$this->option('charset', 'utf8');
$this->option('type', 'InnoDB');
}
public function setUp()
{
parent::setUp();
$this->hasMany('Model_UserTraining as Users_has_Trainings', array(
'local' => 'idTrainings',
'foreign' => 'Trainings_idTrainings'));
$this->hasMany('Model_TrainingBonuse as Trainings_has_Bonuses', array(
'local' => 'idTrainings',
'foreign' => 'Trainings_idTrainings'));
}
}
TRAINING_BONUSES
abstract class Model_Base_TrainingBonuse extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('Trainings_has_Bonuses');
$this->hasColumn('Trainings_idTrainings', 'integer', 4, array(
'type' => 'integer',
'notnull' => true,
'autoincrement' => true,
'length' => '4',
));
$this->hasColumn('Bonuses_idBonuses', 'integer', 4, array(
'type' => 'integer',
'notnull' => true,
'length' => '4',
));
$this->index('fk_Trainings_has_Bonuses_Bonuses1', array(
'fields' =>
array(
0 => 'Bonuses_idBonuses',
),
));
$this->index('fk_Trainings_has_Bonuses_Trainings1', array(
'fields' =>
array(
0 => 'Trainings_idTrainings',
),
));
$this->option('collate', 'utf8_polish_ci');
$this->option('charset', 'utf8');
$this->option('type', 'InnoDB');
}
public function setUp()
{
parent::setUp();
$this->hasOne('Model_Training as TrainingsIdTraining', array(
'local' => 'Trainings_idTrainings',
'foreign' => 'idTrainings'));
$this->hasOne('Model_Bonuse as BonusesIdBonuse', array(
'local' => 'Bonuses_idBonuses',
'foreign' => 'idBonuses'));
}
}
BONUSES
abstract class Model_Base_Bonuse extends Doctrine_Record
{
public function setTableDefinition()
{
$this->setTableName('Bonuses');
$this->hasColumn('idBonuses', 'integer', 4, array(
'type' => 'integer',
'primary' => true,
'autoincrement' => true,
'length' => '4',
));
$this->hasColumn('name', 'string', 45, array(
'type' => 'string',
'length' => '45',
));
$this->hasColumn('title', 'string', 255, array(
'type' => 'string',
'length' => '255',
));
$this->hasColumn('description', 'clob', 65535, array(
'type' => 'clob',
'length' => '65535',
));
$this->hasColumn('file_url_full', 'string', 255, array(
'type' => 'string',
'length' => '255',
));
$this->hasColumn('file_type', 'integer', 1, array(
'type' => 'integer',
'length' => '1',
));
$this->hasColumn('width_full', 'integer', 4, array(
'type' => 'integer',
'length' => '4',
));
$this->hasColumn('height_full', 'integer', 4, array(
'type' => 'integer',
'length' => '4',
));
$this->hasColumn('active', 'integer', 1, array(
'type' => 'integer',
'default' => 1,
'length' => '1',
));
$this->option('collate', 'utf8_polish_ci');
$this->option('charset', 'utf8');
$this->option('type', 'InnoDB');
}
public function setUp()
{
parent::setUp();
$this->hasMany('Model_TrainingBonuse as Trainings_has_Bonuses', array(
'local' => 'idBonuses',
'foreign' => 'Bonuses_idBonuses'));
}
}
There's no "Model_Training" table definition in your schema.yml file...theres "Training" though... If there's no "alias class" or "foreign alias" element specified in your schema.yml, table definition's name is used. So instead of "Model_Training" use "Training" (this applies to "Model_Bonuses" and others too).

Resources